Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collector

 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How many objects will be eligible for GC after the method returns?


the answer says only one ("a" in line 1) is garbage collected.
but what about line 2 and 3?
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have just found this:

The objects created in the String pool are not subjected to GC until the class is unloaded by the JVM. They get discarded at that time or just before the virtual machine unloads the class' bytecode.

Is that the reason why "str" in line 3 is not GCted?
 
Miki Muzsi
Ranch Hand
Posts: 120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but what about line 2 and 3?

line 2 declares a primitive variable, not an object! And the question is "How many objects will be eligible .... Anyway "i" is put on the stack so it gets deleted as soon as is out of the scupe (function in this case).
About String literals you already figured out the problem.
Miki
 
Marlene Miller
Ranch Hand
Posts: 1392
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andres, I have never read such a definitive statement about garbage collection and the string literals. I am curious to know the source of that statement. Marlene
 
Jose Botella
Ranch Hand
Posts: 2120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marlene I do not know where Andres has read that, but this post is an example.
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry guys for giving the wrong resource.. I was checking my notes and I found this statement here instead of JQplus.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic