• Post Reply Bookmark Topic Watch Topic
  • New Topic

Garbage Collection  RSS feed

 
kaiz jessani
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • 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
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
if line 1--1 object
if line 2--2 objects
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
line1--->g1
line2--->g1,L1
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think 4 objects: L, L1, g1, String
 
kaiz jessani
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • 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: 934
4
  • Mark post as helpful
  • send pies
  • 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
  • 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: 934
4
  • Mark post as helpful
  • send pies
  • 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
  • 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 !!!
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

As already mentioned, please QuoteYourSources.

Henry
 
kaiz jessani
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please answer
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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 ...
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • 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?
 
Luke Leber
Ranch Hand
Posts: 69
2
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Please answer about only line1??
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!