What happens with object on which pointed first element of array (array). 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ł
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
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.