next-fit algorithm spreads fragmentation more evenly throughout the memory heapthus keeping search times low this spreading also makes it more difficult to allocate large blockshowever the worst-fit algorithm attempts to avoid this problem by keeping contiguous sections of free memory as large as possible garbage collection in some languageslike and ++the memory space for objects must be explicitly deallocated by the programmerwhich is duty often overlooked by beginning programmers and is the source of frustrating programming errors even for experienced programmers insteadthe designers of java placed the burden of memory management entirely on the run-time environment as mentioned abovememory for objects is allocated from the memory heap and the space for the instance variables of running java program are placed in its method stacksone for each running thread (for the simple programs discussed in this book there is typically just one running threadsince instance variables in method stack can refer to objects in the memory heapall the variables and objects in the method stacks of running threads are called root objects all those objects that can be reached by following object references that start from root object are called live objects the live objects are the active objects currently being used by the running programthese objects should not be deallocated for examplea running java program may storein variablea reference to sequence that is implemented using doubly linked list the reference variable to is root objectwhile the object for is live objectas are all the node objects that are referenced from this object and all the elements that are referenced from these node objects from time to timethe java virtual machine (jvmmay notice that available space in the memory heap is becoming scarce at such timesthe jvm can elect to reclaim the space that is being used for objects that are no longer liveand return the reclaimed memory to the free list this reclamation process is known as garbage collection there are several different algorithms for garbage collectionbut one of the most used is the mark-sweep algorithm in the mark-sweep garbage collection algorithmwe associate "markbit with each object that identifies if that object is live or not when we determine at some point that garbage collection is neededwe suspend all other running threads and clear the mark bits of all the objects currently allocated in the memory heap we then trace through the java stacks of the currently running threads and we mark all the (rootobjects in these stacks as "live we must then determine all the other live objects--the ones that are reachable from the root objects to do this efficientlywe can use the directed-graph version of the depth-first search traversal (section in this caseeach object in the memory heap is viewed as vertex in directed graphand the reference from one object to another is viewed as directed edge by performing directed dfs from each root objectwe can correctly