how many objects r eligible for garbage Collection ?

N

Naveen Kumar

I am confused by someone over object creation in java.. Here is a
sample question..

class Card {
Short story=5;
Card go(Card c)
{
c=null;
return c;
}

public static void main(String[] args)
{
Card c1=new Card();
Card c2=new Card();
Card c3=c1.go(c2);
c1=null;
//do stuff;
}

when //do stuff is reached How many Objects are eligible for garbage
Collection ?

Does c3 occupies memory and is eligible for garbage collection..
When we say

Card c3 = null;

Isnt memory allocated to this reference for storing valid object.. and
is now available for gc .. ???
Wht happens in case when i say Card c3 = null ?? Isnt heap and stack
space utilized ?
 
L

Lew

Naveen said:
I am confused by someone over object creation in java [sic].. Here is a
sample question..

It's spelled "Java".
class Card {

Why not public?
Short story=5;

Please follow the indentation conventions in order to make your code more
readable.

Why is 'story' not private?
Card go(Card c)
{
c=null;
return c;
}

public static void main(String[] args)
{
Card c1=new Card();
Card c2=new Card();
Card c3=c1.go(c2);
c1=null;
//do stuff;
}

when //do stuff is reached How many Objects are eligible for garbage
Collection ?

Is this homework?

Here's how to solve this one: Identify all the object references in the
program at that point. (Is 'story' one of them?) See which objects /used to
have/ references but no longer do. Those are the ones eligible for GC.

It might help to write down for each line of code which objects exist, and
which ones from previous lines no longer have references to them.

Follow the whole chain - if an object holds a reference to another object,
there's another object to track.
Does c3 occupies memory and is eligible for garbage collection..

What is the value of c3?

Hint: null is not an object.
When we say

Card c3 = null;

Isnt memory allocated to this reference for storing valid object.. and
is now available for gc .. ???

Reference to what? c3 is null, so it doesn't point to any object, valid or not.
Wht [sic] happens in case when i [sic] say Card c3 = null ?? Isnt heap and stack
space utilized ?

What do you mean by "utilized"?

What happens is that the variable 'c3' is cleared of any reference to any object.
 
N

Naveen Kumar

Naveen said:
I am confused by someone over object creation in java [sic].. Here is a
sample question..

It's spelled "Java".

Well Lew, Are you programmer or high school grammar teacher [sick].
Why not public?

Why not default ? Does it cost you if I dont use public ?

Please follow the indentation conventions in order to make your code more
readable.

Make sense to certain extent, but this code was for example and it
cannot be used in eclipse to get the expected answer.
Why is 'story' not private?

By not using 'private' is your privacy lost ? Cant Java variable be
declared without being private ?
Card go(Card c)
{
c=null;
return c;
}
public static void main(String[] args)
{
Card c1=new Card();
Card c2=new Card();
Card c3=c1.go(c2);
c1=null;
//do stuff;
}
when //do stuff is reached How many Objects are eligible for garbage
Collection ?

Is this homework?

You never had any doubts in your life ? You dont have to sit in high
school to get your doubts cleared.
Here's how to solve this one: Identify all the object references in the
program at that point. (Is 'story' one of them?) See which objects /used to
have/ references but no longer do. Those are the ones eligible for GC.

It might help to write down for each line of code which objects exist, and
which ones from previous lines no longer have references to them.

Follow the whole chain - if an object holds a reference to another object,
there's another object to track.


What is the value of c3?

Hint: null is not an object.

Yes, but the confusion began because of it. What in case if i
do not initialize the object as null. Have you ever tried using an
variable without initializing it ? Like

Card c3;
This will throw error where as

Card c3 = null;

will never throw the error. Something happens in background. What
exactly happens which make this work and not the previous one ?
Reference to what? c3 is null, so it doesn't point to any object, valid or not.

What exactly do you call c3 now if not object ? If c3 is not an
object, what is it ?
Wht [sic] happens in case when i [sic] say Card c3 = null ?? Isnt heap and stack
space utilized ?

What do you mean by "utilized"?

If you do not understand the word "utilized", guess you can sit and
recollect your class 2 course.
What happens is that the variable 'c3' is cleared of any reference to any object.

Well, everyone knows about that. But the question was something else.

Thanks but no thanks for your grammatical correction. You were
expected to answer technical stuff rather than grammar corrections.
Answer the technical question if you can, else you can continue with
your work.
 
N

Naveen Kumar

Anyways, I got the answer. For others, there is just one object for
gc, which is c1. c3 is just an null reference.

Cheers
Naveen Kumar

Naveen said:
I am confused by someone over object creation in java [sic].. Here is a
sample question..
It's spelled "Java".

Well Lew, Are you programmer or high school grammar teacher [sick].


Why not public?

Why not default ? Does it cost you if I dont use public ?
Please follow the indentation conventions in order to make your code more
readable.

Make sense to certain extent, but this code was for example and it
cannot be used in eclipse to get the expected answer.
Why is 'story' not private?

By not using 'private' is your privacy lost ? Cant Java variable be
declared without being private ?




Card go(Card c)
{
c=null;
return c;
}
public static void main(String[] args)
{
Card c1=new Card();
Card c2=new Card();
Card c3=c1.go(c2);
c1=null;
//do stuff;
}
when //do stuff is reached How many Objects are eligible for garbage
Collection ?
Is this homework?

You never had any doubts in your life ? You dont have to sit in high
school to get your doubts cleared.




Here's how to solve this one: Identify all the object references in the
program at that point. (Is 'story' one of them?) See which objects /used to
have/ references but no longer do. Those are the ones eligible for GC.
It might help to write down for each line of code which objects exist, and
which ones from previous lines no longer have references to them.
Follow the whole chain - if an object holds a reference to another object,
there's another object to track.
What is the value of c3?
Hint: null is not an object.

Yes, but the confusion began because of it. What in case if i
do not initialize the object as null. Have you ever tried using an
variable without initializing it ? Like

Card c3;
This will throw error where as

Card c3 = null;

will never throw the error. Something happens in background. What
exactly happens which make this work and not the previous one ?


Reference to what? c3 is null, so it doesn't point to any object, valid or not.

What exactly do you call c3 now if not object ? If c3 is not an
object, what is it ?


Wht [sic] happens in case when i [sic] say Card c3 = null ?? Isnt heap and stack
space utilized ?
What do you mean by "utilized"?

If you do not understand the word "utilized", guess you can sit and
recollect your class 2 course.


What happens is that the variable 'c3' is cleared of any reference to any object.

Well, everyone knows about that. But the question was something else.



Thanks but no thanks for your grammatical correction. You were
expected to answer technical stuff rather than grammar corrections.
Answer the technical question if you can, else you can continue with
your work.
 
L

Lasse Reichstein Nielsen

[regarding this program:]
|
| Card go(Card c)
| {
| c=null;
| return c;
| }
|
| public static void main(String[] args)
| {
| Card c1=new Card();
| Card c2=new Card();
| Card c3=c1.go(c2);
| c1=null;
| //do stuff;
| }

....
Yes, but the confusion began because of it. What in case if i
do not initialize the object as null. Have you ever tried using an
variable without initializing it ? Like

Card c3;
This will throw error where as

It will fail to compile.
Card c3 = null;
will never throw the error. Something happens in background. What
exactly happens which make this work and not the previous one ?

Local variable may not be *read* before they are initialized (i.e., a
value is assigned to them). The language specification enforces this
by requireing that allpaths of execution leading to a variable being
read must include an assignment to the variable, and the compiler
checks this requirement. Nothing happens in the background.
What exactly do you call c3 now if not object ? If c3 is not an
object, what is it ?

c3 is a variable.
It holds a value. Or rather, the location in the runtime environment
that corresponds to the variable, holds a value.
The type of that value must be a reference to a Card object.
The value itself is null, which is a valid value of that type.

No objects are referenced by c3's value.
If you do not understand the word "utilized", guess you can sit and
recollect your class 2 course.

What do *you* mean by "utilizied" in *this* case? I.e., your question
was not clear.

The variable c3 is probably mapped to a stack location in the runtime
environment. Assigning the null value to it will store the corresponding
value in that stack location.

/L
 
L

Lasse Reichstein Nielsen

Naveen Kumar said:
Anyways, I got the answer. For others, there is just one object for
gc, which is c1. c3 is just an null reference.

Or more pedantically: The only object eligable for garbage collection
is the one originally assigned to the variable c1. When trying to
understand garbage collection, it's important to distinguish between
objects, references to objects and variables holding references.

/L
 
L

Lew

Lasse said:
Or more pedantically: The only object eligable for garbage collection
is the one originally assigned to the variable c1. When trying to
understand garbage collection, it's important to distinguish between
objects, references to objects and variables holding references.

That's not true. You missed one other.
 
L

Lew

Naveen said:
Why not default ? Does it cost you if I dont use public ?

Do you have some trouble answering other people's questions, or do you regard
Usenet as your personal private help desk?

I asked so I could understand, and perhaps learn from your reasoning. Aren't
you willing to share your insights?
Make sense to certain extent, but this code was for example and it
cannot be used in eclipse to get the expected answer.

Huh? What does that have to do with indenting so that the people you're
asking for help have an easier time providing it?
By not using 'private' is your privacy lost ? Cant Java variable be
declared without being private ?

Boy, you sure are unwilling to participate in discussion, aren't you?

Why are you so averse to providing your insights and reasoning?
Card go(Card c)
{
c=null;
return c;
}
public static void main(String[] args)
{
Card c1=new Card();
Card c2=new Card();
Card c3=c1.go(c2);
c1=null;
//do stuff;
}
when //do stuff is reached How many Objects are eligible for garbage
Collection ?
Is this homework?
You never had any doubts in your life ? You dont have to sit in high
school to get your doubts cleared.

So that's a "yes"?
Yes, but the confusion began because of it. What in case if i
do not initialize the object as null. Have you ever tried using an
variable without initializing it ? Like

Card c3;
This will throw error where as

Card c3 = null;

will never throw the error. Something happens in background. What
exactly happens which make this work and not the previous one ?

This is explained in the Java Language Specification (JLS).

The variable 'c3' must be assigned either an object reference or null.
What exactly do you call c3 now if not object ? If c3 is not an
object, what is it ?

It's a variable. Variables and objects are different things.
Wht [sic] happens in case when i [sic] say Card c3 = null ?? Isnt heap and stack
space utilized ?
What do you mean by "utilized"?

If you do not understand the word "utilized", guess you can sit and
recollect your class 2 course.

Boy, /that/ was rude. I was trying to understand your question, but you come
back with snide hostility. O-kaaay.
Well, everyone knows about that. But the question was something else.

Funny, you were asking what happens, so I told you. It wasn't clear from your
question that you already knew the answer.
Thanks but no thanks for your grammatical correction. You were
expected to answer technical stuff rather than grammar corrections.
Answer the technical question if you can, else you can continue with
your work.

My, my, we're bossy today! I am just astounded by how rude you are.

It is certainly not anyone's job on Usenet to answer your questions, nor even
to acknowlege them. This is a *discussion* group, not a help desk. You are
not winning friends and influencing people with your high-handed attitude,
Dale Carnegie.

Why don't you lay off the arrogance and participate in the discussions like a
participant, hm?
 
T

Tim Smith

Why not public?

Does it matter?
Please follow the indentation conventions in order to make your code more
readable.

Why is 'story' not private?

Does it matter?

....
What is the value of c3?

Hint: null is not an object.


Reference to what? c3 is null, so it doesn't point to any object, valid or
not.

But it takes memory to hold that null. The original poster is a bit
unclear on how memory works in Java (else he wouldn't be asking these
questions), so quite possibly he wants to know if that memory is
eligible for garbage collection.
 
M

Mark Thornton

Naveen said:
I am confused by someone over object creation in java.. Here is a
sample question..

class Card {
Short story=5;
Card go(Card c)
{
c=null;
return c;
}

public static void main(String[] args)
{
Card c1=new Card();
Card c2=new Card();
Card c3=c1.go(c2);
c1=null;
//do stuff;
}

when //do stuff is reached How many Objects are eligible for garbage
Collection ?

Unless c2 is used in 'do stuff' then all the objects are eligible for
collection. Merely having a local reference to an object is not
sufficient to keep the object. Local references are deemed not to exist
beyond their last point of use. This is not just theoretical either, the
current Java 6 from Sun does this.

Mark Thornton
 
A

Andreas Leitgeb

Lew said:
I asked so I could understand, and perhaps learn from your reasoning. Aren't
you willing to share your insights?

I'm not really willing to comment about homework or not, or
about the asker's behaviour, but this particular point seemed
quite obvious to me. It was a simple code-sample (almost an
SSCCE), and anything else than default access would have added
unnecessary (in this context) keywords :)
 
R

Roger Lindsjö

Lew said:
The Short which is referenced within the Card.

The Card object references by c2 might be available too, depending on
what "// do stuff" does with it. Many (all?) GC implementations seem to
not collect objects referenced by variables still on the stack, but I
don't think they are forced to.

//Roger Lindsjö
 
P

Patricia Shanahan

Mark said:
Naveen said:
I am confused by someone over object creation in java.. Here is a
sample question..

class Card {
Short story=5;
Card go(Card c)
{
c=null;
return c;
}

public static void main(String[] args)
{
Card c1=new Card();
Card c2=new Card();
Card c3=c1.go(c2);
c1=null;
//do stuff;
}

when //do stuff is reached How many Objects are eligible for garbage
Collection ?

Unless c2 is used in 'do stuff' then all the objects are eligible for
collection. Merely having a local reference to an object is not
sufficient to keep the object. Local references are deemed not to exist
beyond their last point of use. This is not just theoretical either, the
current Java 6 from Sun does this.
....

More generally, I think this thread has failed to fully distinguish two
concepts:

1. Is an object unreachable, and therefore eligible for finalization?
The test here is whether the object can be accessed by any continuing
computation in any active thread. This is a property of the program and
the language specification, regardless of JVM implementation.

2. Will a particular JVM GC implementation, when dealing with a memory
shortage, notice that, and collect it. This is subject to change as JVMs
become more sophisticated.

Unreachability is not, in general, computable. The JVM has to
approximate it. Given the disastrous consequences of collecting a
reachable object, it has to err in the direction of not collecting some
unreachable objects. There will never be a perfect garbage collector
that collects every unreachable object and no others.

Patricia
 
M

Mark Thornton

Roger said:
The Card object references by c2 might be available too, depending on
what "// do stuff" does with it. Many (all?) GC implementations seem to
not collect objects referenced by variables still on the stack, but I
don't think they are forced to.

Java 6 does collect objects where the only references have no further
use. It is possible to observe this behaviour if you also have a weak
reference to the 'garbage'.

Mark Thornton
 
L

Lew

Mark said:
Java 6 does collect objects where the only references have no further
use. It is possible to observe this behaviour if you also have a weak
reference to the 'garbage'.

This impinges on another thread discussing variables that are not unstacked
after their block until the end of their method. Someone proposed that
nulling such a variable would "help" GC; the observation here is that such
"help" is completely unnecessary.
 
L

Lew

Andreas said:
I'm not really willing to comment about homework or not, or
about the asker's behaviour, but this particular point seemed
quite obvious to me. It was a simple code-sample (almost an
SSCCE), and anything else than default access would have added
unnecessary (in this context) keywords :)

I was wondering if that was a reason, or if there were others I hadn't thought
of. It's always edifying to find out why people do things, especially if they
are unexpected things.

One could argue that leaving off the expected "public" keyword actually
diminishes the pedagogical value of an example, as the unusual idiom actually
distracts one from whatever the primary purpose of the example is.

Inquiring minds simply want to know. Andreas's guess is plausible.
 
M

Mark Thornton

Patricia said:
More generally, I think this thread has failed to fully distinguish two
concepts:

1. Is an object unreachable, and therefore eligible for finalization?
The test here is whether the object can be accessed by any continuing
computation in any active thread. This is a property of the program and
the language specification, regardless of JVM implementation.

E.g. a reference that would only be used if System.currentTimeMillis()
returned a negative number might be considered 'dead', but I doubt we
will ever see a JVM that recognises that.

Mark Thornton
 
L

Lew

Tim said:
Does it matter?
Yes.


Does it matter?
Yes.


But it takes memory to hold that null. The original poster is a bit
unclear on how memory works in Java (else he wouldn't be asking these
questions), so quite possibly he wants to know if that memory is
eligible for garbage collection.

There is no such thing as GCing a null, so no, memory to hold a null reference
is not eligible for GC.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top