23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Memory Allocation <strong>Algorithms</strong><br />

The <strong>Java</strong> Virtual Mach<strong>in</strong>e def<strong>in</strong>ition requires that the memory heap be able to<br />

quickly allocate memory for new objects, but it does not specify the data structure<br />

that we should use to do this. One popular method is to keep contiguous "holes"<br />

of available free memory <strong>in</strong> a doubly l<strong>in</strong>ked list, called the free list. The l<strong>in</strong>ks<br />

jo<strong>in</strong><strong>in</strong>g these holes are stored <strong>in</strong>side the holes themselves, s<strong>in</strong>ce their memory is<br />

not be<strong>in</strong>g used. As memory is allocated <strong>and</strong> deallocated, the collection of holes <strong>in</strong><br />

the free lists changes, with the unused memory be<strong>in</strong>g separated <strong>in</strong>to disjo<strong>in</strong>t holes<br />

divided by blocks of used memory. This separation of unused memory <strong>in</strong>to<br />

separate holes is known as fragmentation. Of course, we would like to m<strong>in</strong>imize<br />

fragmentation as much as possible.<br />

There are two k<strong>in</strong>ds of fragmentation that can occur. Internal fragmentation<br />

occurs when a portion of an allocated memory block is not actually used. For<br />

example, a program may request an array of size 1000, but only use the first 100<br />

cells of this array. There isn't much that a run-time environment can do to reduce<br />

<strong>in</strong>ternal fragmentation. External fragmentation, on the other h<strong>and</strong>, occurs when<br />

the there is a significant amount of unused memory between several contiguous<br />

blocks of allocated memory. S<strong>in</strong>ce the run-time environment has control over<br />

where to allocate memory when it is requested (for example, when the new<br />

keyword is used <strong>in</strong> <strong>Java</strong>), the run-time environment should allocate memory <strong>in</strong> a<br />

way that tries to reduce external fragmentation as much as reasonably possible.<br />

Several heuristics have been suggested for allocat<strong>in</strong>g memory from the heap so as<br />

to m<strong>in</strong>imize external fragmentation. The best-fit algorithm searches the entire<br />

free list to f<strong>in</strong>d the hole whose size is closest to the amount of memory be<strong>in</strong>g<br />

requested. The first-fit algorithm searches from the beg<strong>in</strong>n<strong>in</strong>g of the free list for<br />

the first hole that is large enough. The next-fit algorithm is similar, <strong>in</strong> that it also<br />

searches the free list for the first hole that is large enough, but it beg<strong>in</strong>s its search<br />

from where it left off previously, view<strong>in</strong>g the free list as a circularly l<strong>in</strong>ked list<br />

(Section 3.4.1). The worst-fit algorithm searches the free list to f<strong>in</strong>d the largest<br />

hole of available memory, which might be done faster than a search of the entire<br />

free list if this list were ma<strong>in</strong>ta<strong>in</strong>ed as a priority queue (Chapter 8). In each<br />

algorithm, the requested amount of memory is subtracted from the chosen<br />

memory hole <strong>and</strong> the leftover part of that hole is returned to the free list.<br />

Although it might sound good at first, the best-fit algorithm tends to produce the<br />

worst external fragmentation, s<strong>in</strong>ce the leftover parts of the chosen holes tend to<br />

be small. The first-fit algorithm is fast, but it tends to produce a lot of external<br />

891

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!