• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

GC is Back Again

 
Ranch Hand
Posts: 114
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Code is written by me.

Guess How Many Objects are eligible for GC?? ;)
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
5 were eligible after execution of the main method.
 
Aashu Mahajan
Ranch Hand
Posts: 114
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes there is total 5 objects are eligible for GC
 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Aashu,

how you make it as 5?Please explain.
 
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Guys, there are SIX objects 'eligible' for garbaje collection when execution gets to line 22. Or better said after the execution of the for loop.

This code 'as-it-stands' the for loop executes TWICE creating a total of six objects on the heap.

REMEMBER that 'local' reference variables (when I say local I mean created in methods or block of codes) become 'inexitent', that is they disappear from the stack after execution of the methods or block of codes, hence objects created in that environment also become eligible for garbage collection. SAME applies to objects created in inner classes.

REMEBER that there is ONLY one static variable 'gcp' available to all instances of a class and what ever value it has at 'any-given-time' is shared by all instances of the class as long as the class lives.


Regards

Ikpefua
 
Aashu Mahajan
Ranch Hand
Posts: 114
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI, Ikpefua Jacob-Obinyan

There are total six objects created and at last 5 objects will be eligible for GC because one object will still referred to by static variable 'gcp'. You can try by implementing finalize() method.
Lets check and let me know where i am wrong. I am waiting for reply.
Thanks
 
Aashu Mahajan
Ranch Hand
Posts: 114
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI, Ikpefua Jacob-Obinyan and Muneeswaran Balasubramanian , I implemented finalize() method in the code, run it and let me know.
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Guys,
the finalize method effectively says 5 objects are eligible for garbage collection, @ Aashu now what is STRANGE to me is that the 'null' passed into the constructor at the end of line 12 of your code, the assingment operation to null does NOT seem to have taken effect, because if it does it would have been six objects eligible for garbage collection, now the method System.gc() is NOT on the exams because its function is NOT guarranteed and the certification exams (except Threads) are interested in guarranteed behaviour, that said, I will appreciate it if someone can explain to us why the 'null' in line 12 of this program is NOT assinged to the static variable gcp after the execution of line 12, so there is something I am mixing up here about how line 12 works (Regarding static variables) However I will take my time and investigate where I went wrong.

Regards

Ikpefua
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ikpefua, you are correct that six objects are created, but only 5 will be eligible. In the second loop of the program, during the getGC() method, two objects are created. When the 'inner' object is created, it will set the static variable to null. However, when the 'outer' object is created, it will assign to the static variable the 'inner' object we just created. So at the end, the program will still have a reference to this object.
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Stephan what amazes me is that with 'non-static' instance variables the 'reverse' of what you explained seems to be the case. I slightly modified the program take a CLOSE look at the output:


Output: when the 'static' instance variable IS commented and 'non-static' instance variable NOT commented
6 objects are 'eligible'.


Output: when the 'static' instance variable is NOT commented and 'non-static' instance variable IS
commented, 5 objects are 'eligible'.


GCP_Heap.png
[Thumbnail for GCP_Heap.png]
When gcp is non_static
GCP_Heap_2.png
[Thumbnail for GCP_Heap_2.png]
When gcp is static
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I 'think' I got the answer!...@Stephan your theory is correct, the 'reverse' is NOT the case, 'static' variables are alive as long as the 'class' is alive, while 'non-static' are alive as long as the 'object' is alive.
 
Aashu Mahajan
Ranch Hand
Posts: 114
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Stephan van Hulst
@Ikpefua Jacob-Obinyan :
 
The world's cheapest jedi mind trick: "Aw c'mon, why not read this tiny ad?"
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic