The new operator is relatively expensive to use.
Garbage collection is also expensive.
A Memory Management
handles general-purpose memory requests.
The expense comes from the fact that free store routines must
be capable of handling requests to and from free store with no
particular pattern, as well as memory requests of vastly different
sizes.
This, combined with unpredictable freeing of space by the garbage
collector, makes them inefficient compared to what might be
implemented for more controlled patterns of memory access.
List nodes are created and deleted in a linked list
implementation in a way that allows the Link class programmer
to provide simple but efficient memory management routines.
Instead of making repeated calls to new,
the Link class can handle its own freelist.
A freelist holds those list nodes that are not currently being used.
When a node is deleted from a linked list, it is placed at the
head of the freelist.
When a new element is to be added to a linked list, the freelist
is checked to see if a list node is available.
If so, the node is taken from the freelist.
If the freelist is empty, the standard new operator must then
be called.
So we see that the freelist is simply
an application of a linked stack.
We will illustrate using a freelist by performing a series of list operations. Let's start from an empty singly linked list and a freelist variable pointing to null.
Freelists are particularly useful for linked lists that periodically
grow and then shrink.
The freelist will never grow larger than the largest size yet reached
by the linked list.
Requests for new nodes (after the list has shrunk) can be handled by
the freelist.
Another good opportunity to use a freelist occurs when a program uses
multiple lists.
So long as they do not all grow and shrink together, the free list can
let link nodes move between the lists.
In the implementation shown here, the Link class is augmented with
methods get and release. [1]
classLink{// Singly linked list node with freelist supportprivateObjecte;// Value for this nodeprivateLinkn;// Point to next node in list// ConstructorsLink(Objectit,Linkinn){e=it;n=inn;}Link(Linkinn){e=null;n=inn;}Objectelement(){returne;}// Return the valueObjectsetElement(Objectit){returne=it;}// Set element valueLinknext(){returnn;}// Return next linkLinksetNext(Linkinn){returnn=inn;}// Set next link// Extensions to support freelistsstaticLinkfreelist=null;// Freelist for the class// Return a new link, from freelist if possiblestaticLinkget(Objectit,Linkinn){if(freelist==null)returnnewLink(it,inn);// Get from "new"Linktemp=freelist;// Get from freelistfreelist=freelist.next();temp.setElement(it);temp.setNext(inn);returntemp;}// Return a link node to the freelistvoidrelease(){e=null;// Drop reference to the elementn=freelist;freelist=this;}}
classLink<E>{// Singly linked list node with freelist supportprivateEe;// Value for this nodeprivateLink<E>n;// Point to next node in list// ConstructorsLink(Eit,Link<E>inn){e=it;n=inn;}Link(Link<E>inn){e=null;n=inn;}Eelement(){returne;}// Return the valueEsetElement(Eit){returne=it;}// Set element valueLink<E>next(){returnn;}// Return next linkLink<E>setNext(Link<E>inn){returnn=inn;}// Set next link// Extensions to support freelistsprivatestaticLinkfreelist=null;// Freelist for the class// Return a new link, from freelist if possiblestatic<E>Link<E>get(Eit,Link<E>inn){if(freelist==null){returnnewLink<E>(it,inn);// Get from "new"}Link<E>temp=freelist;// Get from freelistfreelist=freelist.next();temp.setElement(it);temp.setNext(inn);returntemp;}// Return a link node to the freelistvoidrelease(){e=null;// Drop reference to the elementn=freelist;freelist=this;}}
The freelist variable declaration uses the keyword static.
This creates a single variable shared among all instances of the
Link nodes.
In this way, a single freelist is shared by all Link nodes.
Note how simple they are, because they need only remove and add an
element to the front of the freelist, respectively.
The freelist methods get and release both run in
Θ(1) time, except in the case where the freelist is
exhausted and the new operation must be called.
Here are the necessary modifications to members of the linked list
class to make use of the freelist version of the link class.
// Insert "it" at current positionpublicbooleaninsert(Objectit){curr.setNext(Link.get(curr.element(),curr.next()));// Get linkcurr.setElement(it);if(tail==curr)tail=curr.next();// New taillistSize++;returntrue;}// Append "it" to listpublicbooleanappend(Objectit){tail.setNext(Link.get(null,null));tail.setElement(it);tail=tail.next();listSize++;returntrue;}// Remove and return current elementpublicObjectremove(){if(curr==tail)returnnull;// Nothing to removeObjectit=curr.element();// Remember valuecurr.setElement(curr.next().element());// Pull forward the next elementif(curr.next()==tail)tail=curr;// Removed last, move tailLinktempptr=curr.next();// Remember the linkcurr.setNext(curr.next().next());// Point around unneeded linktempptr.release();// Release the linklistSize--;// Decrement element countreturnit;// Return value}
// Insert "it" at current positionpublicbooleaninsert(Eit){curr.setNext(Link.get(curr.element(),curr.next()));// Get linkcurr.setElement(it);if(tail==curr){tail=curr.next();}// New taillistSize++;returntrue;}// Append "it" to listpublicbooleanappend(Eit){Link<E>temp=Link.get(null,null);tail.setNext(temp);tail.setElement(it);tail=tail.next();listSize++;returntrue;}// Remove and return current elementpublicEremove(){if(curr==tail){returnnull;}// Nothing to removeEit=curr.element();// Remember valuecurr.setElement(curr.next().element());// Pull forward the next elementif(curr.next()==tail){tail=curr;}// Removed last, move tailLink<E>tempptr=curr.next();// Remember the linkcurr.setNext(curr.next().next());// Point around unneeded linktempptr.release();// Release the linklistSize--;// Decrement element countreturnit;// Return value}
How much time is saved by using freelists depends on the language that
you are programming in.
In a language like C++ where the programmer must call new and
delete to manage memory, getting a node from your own freelist
requires less than one tenth of the time required by the new
operator.
In a language like Java that uses garbage collection, it might at
first appear that using your own freelist saves no time, because
Java’s new operator can quickly return space from the current
start of its memory pool.
However, when you do not use a freelist, dropping access to nodes
creates garbage which leads to expensive processing at garbage
collection time.