aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage collection - arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage collection - arrays" Watch "Garbage collection - arrays" New topic
Author

Garbage collection - arrays

Michal Adamski
Greenhorn

Joined: May 30, 2007
Posts: 5
Integer[] array = new Integer[2];
array[0] = 4;
array = null;

What happens with object on which pointed first element of array (array[0]). Is it eligible to GC? If I set refer array to null are the all elements of array are set to null as well?
So my question is, wheter is eligible or not and why.
Thanks very much for help!
Michał
Michal Adamski
Greenhorn

Joined: May 30, 2007
Posts: 5
And how is with collections?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
"If I set refer array to null are the all elements of array are set to null as well?"

No, but yes, the Integer[2] array object and the single Integer object are both eligible for garbage collection.

What you have done is created what is known as "an island of isolated objects", which can be garbage collected.

It would be the same if you used an ArrayList<Integer> in place of an Integer[] array.
[ June 09, 2007: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
If you did this


then the Integer[2] array object is eligible for garbage collection, but the Integer object is not because the variable four still refers to it.
[ June 09, 2007: Message edited by: Barry Gaunt ]
Michal Adamski
Greenhorn

Joined: May 30, 2007
Posts: 5
Thanks for help!
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

Hi,
in continuation to this topic, i would like to ask, how does the GC come to know about which object to garbage collect ?
Let me clarify my question

say if you create an ArrayList to hold say 1000 elements.
you assign proper values to all of them, means they consume memory.
now you assign the reference to some other object. so now there is no reference pointing to the previous ArrayList.
So how does the GC come to know about the previous 1000 element memory?
Is this not a memory leak ?
Or is it like that all the memory used by the Java program is freed at time of termination?
Please guide

Thanks in advance..


SCJP, SCWCD.
|Asking Good Questions|
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Amit, when a reference variable gets assigned a value a lot more goes on behind the scenes than we need to know for SCJP. To put it simply, the Java Virtual Machine is performing various memory management functions which maintain reference counts for the various chunks of heap memory allocated to objects. At sometime in the program's life, the reference counts of a particular chunk of heap memory may go to down to zero as reference variables get set to new values. (Update: it seems that the majority of JVMs do not use reference counting, but have other ways of tracking reachability. Reference counting remains a feasible way of doing it, however.) Depending on the implementation of the JVM, the memory may be immediately made available for a new object or left hanging around for some kind of (unspecified by the JVM specification) garbage collection process to return the memory to a free memory list. Google for something like "Java Heap Memory Management" for more detailed information.

For example this from IBM or this from Sun
[ June 10, 2007: Message edited by: Barry Gaunt ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage collection - arrays