• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage collection - arrays

 
Michal Adamski
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And how is with collections?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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 ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for help!
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic