• 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

Garbage Collection

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Garbage{
static int c;
Long L=45;//line1
Long L1=new Long("45");//line2
public static void main(String... a){
Garbage g1=new Garbage();
g1=null;
}
}

//How many objects are eligible for garbage collection
for respective line 1 and line 2
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We think it generally works better if you tell us what you think first, rather than someone just handing you the answer.

Also, we'd appreciate it if you'd quote your source - tell us where this question came from.
 
kaiz jessani
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if line 1--1 object
if line 2--2 objects
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you tell us what object you think is available on line 1?
 
kaiz jessani
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
line1--->g1
line2--->g1,L1
 
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think 4 objects: L, L1, g1, String
 
kaiz jessani
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tushar Goel wrote:I think 4 objects: L, L1, g1, String


i am asking if line 1 and line 2 were added individually !!
from where did string come???
 
Tushar Goel
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kaiz jessani wrote:
i am asking if line 1 and line 2 were added individually !!
from where did string come???



Line 1 : 1 (L)
Line 2: 2 (L1 and "45") .. 45 is string (I am not sure whether this will be GC or not)
and g1
 
kaiz jessani
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please provide A valid answer..
i m sorry but don't answer if you aren't sure
 
Tushar Goel
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kaiz we all the beginner trying to learn and help others... I may be wrong in some answer but that's why we are here..
 
kaiz jessani
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tushar Goel wrote:Kaiz we all the beginner trying to learn and help others... I may be wrong in some answer but that's why we are here..


sorry ...My bad !!!
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

As already mentioned, please QuoteYourSources.

Henry
 
kaiz jessani
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please answer
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kaiz jessani wrote:please answer


Please EaseUp (←click). I think I've already explained to you that we're all volunteers here; and urgency is your problem, not ours.

I suggest you read the HowToAskQuestionsOnJavaRanch page to learn how to use our forums, because badgering people is not likely to get you answers any quicker - in fact, more likely the reverse.

Winston
 
kaiz jessani
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

kaiz jessani wrote:please answer


Please EaseUp (←click). I think I've already explained to you that we're all volunteers here; and urgency is your problem, not ours.

I suggest you read the HowToAskQuestionsOnJavaRanch page to learn how to use our forums, because badgering people is not likely to get you answers any quicker - in fact, more likely the reverse.

Winston


Sorry for everything.....

i myself created the question.......

once again Sorry ...
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for telling us.

Is the String literal available for garbage collection at any point in the execution of that code?
 
Ranch Hand
Posts: 69
2
Netbeans IDE C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The string literal shall not be eligible for garbage collection given the code that we see. The only way for that string literal to be eligible is for the enclosing class to be both loaded and unloaded dynamically at runtime.

However, even if that was the case, the string would likely be placed in the permanent generation and would not be collected under "normal" program operation -- but it would still be eligible if the permgen were to be collected.

If I can find the section in the standard I'll quote it for you.

I believe your answer lies within ยง4.4 The Constant Pool.

http://docs.oracle.com/javase/specs/jvms/se7/jvms7.pdf
 
kaiz jessani
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please answer about only line1??
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kaiz jessani wrote:Please answer about only line1??


The Java specification does not require that Long object be cached during boxing (or at least, I don't recall it as such). However, the JVM implementation, as first released by Sun, does cache a range of Long instances during autoboxing. This means, that with the current Sun/Oracle implementation, no object is created at line one.

Henry
 
kaiz jessani
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So for line 1-->only 1 object is eligible for garbage collection(g1)
So for line 2--> 2 objects are eligible for garbage collection (L1 and g1)

am i right ??
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kaiz jessani wrote:So for line 1-->only 1 object is eligible for garbage collection(g1)
So for line 2--> 2 objects are eligible for garbage collection (L1 and g1)


Kaiz, I suggest you read the UseCodeTags (←click) page because, without them, it's very difficult to know what you mean by "line 1".
However, the fact is that NO objects will be eligible for collection until after the 'g1 = null' line.

The line before it creates 3 objects: g1 and your two Longs. The String literal is an object, but it is created by the compiler AND placed in the String pool, so it won't be eligible until your program exits.

However, unless you're studying for the OCJP exam, you really don't need to know this stuff - and if you are, my advice would be to forget it as soon as you leave the exam room, because it's simply not important.

Java is a memory-managed language, which means that you leave the business of creating and removing objects up to the JVM and, providing you write your code simply and clearly, the chances are that you will never run into any problems.

An object is available for collection when it goes out of scope or the last variable that references it is set to null. That's all you need to know. All the rest is eyewash, and likely to be more of a distraction than a help.

My 2¢.

Winston
reply
    Bookmark Topic Watch Topic
  • New Topic