• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Self Written Code on Garbage Collection

 
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
Exactly How Many Objects are eligible for GC??



My Answere is total Four Objects are eligible for the garbage Collection. What you say? Please let me know if i am wrong?.
 
Ranch Hand
Posts: 73
Android Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
s1, agc and bgc aren't refered anywhere, so I guess 3 is correct.
s2 being creates in String pool, isn't available for GC
However, one alarming thing in your code "System.gc();" that too 4 times? What are you doing buddy? One is bad enough and you actually wrote 4! I hope you understand the hazards of that statement.
 
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...FWIW with reference to garbage collection 'System.gc()' and 'Strings' are NOT on the exams, that of Strings is logical because of the theory sorrounding string-literal-pool.

However it is a good idea to study this futher, because the more knowledge you acquire, the better 'equipped' you are for your first job.

Regards

Ikpefua.
 
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,

Here only 2 objects(agc,s1) are eligible for garbage collection.Because



So only the obj agc ,s1 is eligible for GC.

Hopes this helps.
 
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
This is new for me, i never ever heard or read anywhere about Integer leteral Pool but Thanks Muneeswaran Balasubramanian

but i want to know why 'bgc' is not eligible for GC. have a look again on the code :

I am talking about Object created on line-5 should also be eligible for GC when 'bgc' becomes equal to null.

@Muneeswaran Balasubramanian : i want some more information about Integer literal pool. Suppose in place of Short if it is like that :





 
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
Please clarify my doubts soon which i asked above this post. i am soon going to write OCJP exam. This is very important for me.
 
Pranav Raulkar
Ranch Hand
Posts: 73
Android Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aashu, bgc is indeed available for GC. Any Object that you create and set it to null makes it available for GC.

There is no Integer literal pool as far as I know, just the String literal pool. Strings created in the following way (without using 'new' keyword) are called string literals.Many people say string literal pool is a collection of String objects which is incorrect. It's a collection of references to String objects. Strings that are part of the String literal pool live on the heap but their references reside in the pool (a table maintained internally). Its a way to share strings. Now its safe to share these string objects as strings are immutable.
outputs true in both cases. Remember, creating a string using new keyword forces JVM to create string on heap rather than using one from string literal pool. String literals always have a reference to them from the String Literal Pool so they are never available for GC. So in your case 3 objects are available for GC, s1 (not a literal string and not referenced anywhere), agc (not referenced anywhere) and bgc (setting object to null makes it available for GC)
 
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
Thank Pranav Raulkar for your response, I totally agree with you.
But you didn't say anything about the 'Line-1' in the code.

A Short object is created on Line-1 it should also be eligible for the GC when 'bgc' becomes null, because Short is also an Object.
 
Pranav Raulkar
Ranch Hand
Posts: 73
Android Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops! Totlly forgot about line 1 You are correct, it becomes eligible for GC too.
 
Pranav Raulkar
Ranch Hand
Posts: 73
Android Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops! Totally forgot about line 1 You are correct, it becomes eligible for GC too.
 
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
Thanks Pranav Raulkar, Now it is true that there is no Integer literal pool and wrapper class objects like Integer, Float, Double, Short whatever they have value assigned CAN BE eligible for GC. right??? Please Correct me.

And I want reply from Muneeswaran Balasubramanian also about clarify their previous statement Please.
Thanks Guys

 
Muneeswaran Balasubramanian
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,

Your wait is over here.Sorry for the late reply.
Thats not applicable for Double and Float.If you want to check over this,Play around this code with the various wrapper type
thats make you bit more knowledgeable.




Hopes this helps.
Happy preparation...
 
Pranav Raulkar
Ranch Hand
Posts: 73
Android Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Aashu, there are no literal pools for wrapper classes like Integer, Float, Double, Short.
And yes they are eligible for GC when they are not referred from anywhere.

Muneeswaran, excellent example with the == for Short, Double and Float.
 
Muneeswaran Balasubramanian
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 Pranav,

Yes Aashu, there are no literal pools for wrapper classes like Integer, Float, Double, Short.
And yes they are eligible for GC when they are not referred from anywhere.





Then how the above code returns true and false,Think over it and reply to me.
 
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, Muneeswaran Balasubramanian

I am Still confused looking at you code.

Does it mean that the Wrapper Class Objects : Byte, Short, Integer, Long, if they have value between -128 to 127 then they reside in the literal pool and CAN'T ever be eligible for GC. Am I right at this point???

And if they have value >127 then they reside on the Heap and CAN be eligible for GC. Right???
 
Muneeswaran Balasubramanian
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,

You are absolutely right now.
 
Pranav Raulkar
Ranch Hand
Posts: 73
Android Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Muneeswaran, You are correct. Also, Aashu Objects are always created on heap, regardless of the fact that they belong to a pool or not.
 
My pie came with a little toothpic holding up this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic