views:

78

answers:

2

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 finished, 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 );

+1  A: 

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).

Justin Ethier
actually i think only lists would be marked for garbage collection, since list does not allocate any memory. Am i right?
pavlos
Yes, good point. Only the memory used by the `list` variable itself would be reclaimed - not any memory used by the actual object that `list` points to.
Justin Ethier
+2  A: 

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.

I hope this helps.

Kathy Van Stone
Thanks a lot! it really helped.
pavlos