Computer Science by Sedgewick Robert Wayne Kevin & Kevin Wayne

Computer Science by Sedgewick Robert Wayne Kevin & Kevin Wayne

Author:Sedgewick, Robert,Wayne, Kevin & Kevin Wayne
Language: eng
Format: epub
Publisher: Pearson Education Limited (US titles)
Published: 2017-11-26T05:00:00+00:00


Orphaned items

Java’s garbage collection policy is to reclaim the memory associated with any objects that can no longer be accessed. In the pop() implementation in our initial implementation ArrayStackOfStrings, the reference to the popped item remains in the array. The item is an orphan—we will never use it again within the class, either because the stack will shrink or because it will be overwritten with another reference if the stack grows—but the Java garbage collector has no way to know this. Even when the client is done with the item, the reference in the array may keep it alive. This condition (holding a reference to an item that is no longer needed) is known as loitering, which is not the same as a memory leak (where even the memory management system has no reference to the item). In this case, loitering is easy to avoid. The implementation of pop() in ResizingArrayStackOfStrings sets the array element corresponding to the popped item to null, thus overwriting the unused reference and making it possible for the system to reclaim the memory associated with the popped item when the client is finished with it.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.