Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

classes managing own memory

Effective Java: Item 6: Eliminate obsolete object references.

Generally speaking, whenever a class manages its own memory, the programmer should be alert for memory leaks. Whenever an element is freed, any object references contained in the element should be nulled out.

I don't think I fully understood the description.

What are the examples of a class managing its own memory - I can think of array, list, maybe map.

Could anyone explain the item in somewhat more details it is there in the book? Thanks

like image 843
Atul Avatar asked May 31 '11 05:05

Atul


1 Answers

One simple example is ArrayList, where, when an element is deleted from the end of the list it has to null the array element, not simply decrease the "last element" index. Otherwise the object removed remains reachable by the ArrayList.

like image 141
Lawrence Dol Avatar answered Sep 28 '22 01:09

Lawrence Dol