1

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
  • Note that Vector is an obsolete data strcture that should be be used anymore. Commented Apr 20, 2010 at 10:06

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

2 Comments

actually i think only lists would be marked for garbage collection, since list does not allocate any memory. Am i right?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.