java memory management
i have the following code snapshot:
public void getResults( String expression, String collection ){
ReferenceList list;
Vector <ReferenceList> lists = new <ReferenceList> Vector();
list = invertedIndex.get( 1 )//invertedIndex is class member
lists.add(list);
}
when the method is finish开发者_如何学编程ed, i supose that the local objects ( list, lists) are "destroyed". Can you tell if the memory occupied by list stored in invertedIndex is released as well? Or does java allocate new memory for list when assigning list = invertedIndex.get( 1 );
Think of list
and lists
as references to objects, not the objects themselves, when working out memory management.
Before the method is called, you have invertedIndex
marking some object. Since it is referenced from a Class (which remain in memory), that object is not available for garbage collection.
Inside the method you create a vector and use lists
to reference it. You also name a portion of the object inside invertedIndex with list
.
After the method is called the two names go out of scope. The created vector has no other reference, so it is available for garbage collection. The object referenced by list
, however, is still referenced indirectly by invertedIndex
, so it is not available for garbage collection.
Yes, after the method returns, lists
would be marked for garbage collection.
list
and invertedIndex
would not be affected, unless the get
operation is destructive (IE, it removes list
from itself and returns it).
精彩评论