• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Help with Garbage Collection

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is this question in chapter 3 of scjp 6 study guide book

The question is:
which are true about objects created in the main() and eligible for Garbage Collection:

class Dozens{
int[] dz={1,2,3,4,5,6,7,8,9,10,11,12};
}
public class Eggs{
public static void main(String[] args){
Dozens[] da= new Dozens[3];
da[0]=new Dozens();
Dozens d=new Dozens();
da[1]=d;
d=null;
da[1]=null;
//line 14
}
}


In the book the answers are five objects are created and this i understand and the other answer is two objects are eligible for the GC
In the explanation it says the two objects are da[1] and it array which is dz.

what i don't understand is why isn't Object 'd' included in the GC because i see it referenced to null
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
d is not an object. it is a reference variable.
da[1] is refering to the same object as d.
when d = null; da[1] still is referencing the object.
when da[1] = null then the object don't have more references.
dz still has a reference, but it is isolated, then it can be garbage collected.

well ... I'm not good to explain xD
 
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



please post within the code tag to make it a bit more readable.
 
Ahmad Hamdan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Leonardo i still don't understand
i know 'd' is a refernce variable but its refering to null so why isn't it eligible for the garbage collection?

and thanks avi for the advice
 
avi sinha
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
at first an array object da lets say o1
da[0]--------->dozen obj+ array obj 2 objects lets say o2 & o3
d ------------->dozen obj +array obj 2 object lets say 04 & o5
da[1] -----------------^ no new objects
d=null no object is eligible
da1[1] = null o4 & o5 are eligible

hence

five objects were created
at line 14 two are eligible for gc

solve the problem by creating a heap i can't show you the one i created for it
 
Ahmad Hamdan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Avi i understand now
 
avi sinha
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you 're welcome buddy
my suggestion : always try to solve gc problems using a heap and always post codes within code tags
 
avi sinha
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is the heap which i created
Noname.jpg
[Thumbnail for Noname.jpg]
heap view
 
Think of how stupid the average person is. And how half of them are stupider than that. But who reads this tiny ad?
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic