File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

can anyone explain me this program, which objects are eligible for garbage collection

 
soujanya Bugatha
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Himai Minh
Ranch Hand
Posts: 1142
4
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After line 19, the object created in line 17, A object, is eligible for GC as well as its B object created at line 4.
 
sarvesh dikonda
Ranch Hand
Posts: 58
Firefox Browser Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Himai Minh wrote:After line 19, the object created in line 17, A object, is eligible for GC as well as its B object created at line 4.

i tried to run the same code with a few modifications just to check whether what you quoted was right or not. The code with modifications is as follows:


As in the above code you can spot that i have just commented the line 22 (i.e. aa=null) just to see if the 'aa' is pointing to the same location (at which the object of class A is stored) as 'a' (in class B) is pointing to.

After compiling and running the code the output i got was -

a reference var points to A@3e25a5
b reference var points to B@19821f
In the main() A@3e25a5

From the output it can be seen that object of class A that was created in the main() is still being referenced by 'a' of class B. So, as per my understanding i don't think any of the object is eligible for GC. Kindly, correct me anyone if i am wrong, with a convincing reason.

I am also aware that, if line 2 is changed to-> static protected B b; and if line 11 is changed to-> static protected A a; i.e. if i want to print the values of each of the references in the main() (i.e. if a i insert System.out.println(A.b+" "+B.a); to main()) then, i will get the output as-

a reference var points to A@3e25a5
b reference var points to B@19821f
In the main() null
B@19821f A@3e25a5

As you can see in the above output that i have removed the comment from line 22 (i.e. aa=null). Kindly help me know if i am correct anyone but with a convincing reason.
 
Himai Minh
Ranch Hand
Posts: 1142
4
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try to uncomment aa=null and make b non-static and see.
Exam question is : which object is eligible for GC after aa =null?

When b is static, it belongs to the class. Static object is not eligible for GC.
 
sarvesh dikonda
Ranch Hand
Posts: 58
Firefox Browser Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Himai Minh wrote:Try to uncomment aa=null and make b non-static and see.


Now as you said, i tried doing that way as you can see in the code above which i posted earlier

Himai Minh wrote:Exam question is : which object is eligible for GC after aa =null?

When b is static, it belongs to the class. Static object is not eligible for GC.


I know that when b is static what happens that's just the latter part that i included in my earlier post just to say that what happens if a 'static' keyword is put.
 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you wanna learn this then it's fine, although this topic is not part of ocjp exam anymore.

-hth
 
Himai Minh
Ranch Hand
Posts: 1142
4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For OCJP 6, GC is not in the scope anymore? I'm not sure.

In case the exam requires it, you'll be asked if an object is eligible for GC after a certain line of code. But there is no guarantee this object is GCed. In KB's study guide, there is no guarantee the VM will GC any object until you exit the program.
If an object has no reference refering to it, then it is eligible for GC. But when it is GCed? We never know.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic