Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > how many objects r eligible for garbage Collection ?

Reply
Thread Tools

how many objects r eligible for garbage Collection ?

 
 
Naveen Kumar
Guest
Posts: n/a
 
      02-01-2008

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 ?
 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      02-01-2008
Naveen Kumar wrote:
> 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.

--
Lew
You only need /one/ punctuation mark at the end of each sentence!!!...!!!...!!!???
The word "I" in English is capitalized.
 
Reply With Quote
 
 
 
 
Naveen Kumar
Guest
Posts: n/a
 
      02-01-2008



On Feb 1, 6:58 pm, Lew <(E-Mail Removed)> wrote:
> Naveen Kumar wrote:
> > 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].

>
> > class Card {

>
> Why not public?
>


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


> > Short story=5;

>
> 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.
>
> > Does c3 occupies memory and is eligible for garbage collection..

>
> 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 ?

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


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.

>
> --
> Lew
> You only need /one/ punctuation mark at the end of each sentence!!!...!!!...!!!???
> The word "I" in English is capitalized.


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.

 
Reply With Quote
 
Naveen Kumar
Guest
Posts: n/a
 
      02-01-2008

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

On Feb 1, 11:48 pm, Naveen Kumar <(E-Mail Removed)> wrote:
> On Feb 1, 6:58 pm, Lew <(E-Mail Removed)> wrote:
>
> > Naveen Kumar wrote:
> > > 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].
>
>
>
> > > class Card {

>
> > Why not public?

>
> Why not default ? Does it cost you if I dont use public ?
>
> > > Short story=5;

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

>
> > > Does c3 occupies memory and is eligible for garbage collection..

>
> > 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 ?
>
>
>
> > > 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.

>
> 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.
>
>
>
> > --
> > Lew
> > You only need /one/ punctuation mark at the end of each sentence!!!...!!!...!!!???
> > The word "I" in English is capitalized.

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


 
Reply With Quote
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      02-01-2008
Naveen Kumar <(E-Mail Removed)> writes:

[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.

>> 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 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
--
Lasse Reichstein Nielsen - http://www.velocityreviews.com/forums/(E-Mail Removed)
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      02-01-2008
Naveen Kumar <(E-Mail Removed)> writes:

> 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
--
Lasse Reichstein Nielsen - (E-Mail Removed)
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      02-02-2008
Lasse Reichstein Nielsen wrote:
> Naveen Kumar <(E-Mail Removed)> writes:
>
>> 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.


That's not true. You missed one other.

--
Lew
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      02-02-2008
Naveen Kumar wrote:
>> Naveen Kumar wrote:
>>> class Card {


Lew wrote:
>> Why not public?
>>

>
> 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?

>>> Short story=5;

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


Huh? What does that have to do with indenting so that the people you're
asking for help have an easier time providing it?

>> Why is 'story' not private?

>
> 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"?

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

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

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


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

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


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?

--
Lew
 
Reply With Quote
 
Naveen Kumar
Guest
Posts: n/a
 
      02-02-2008
On Feb 2, 5:18 am, Lew <(E-Mail Removed)> wrote:
> Lasse Reichstein Nielsen wrote:
> > Naveen Kumar <(E-Mail Removed)> writes:

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

>
> That's not true. You missed one other.
>
> --
> Lew


Which is the other one ?
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      02-02-2008
Naveen Kumar wrote:
> On Feb 2, 5:18 am, Lew <(E-Mail Removed)> wrote:
>> Lasse Reichstein Nielsen wrote:
>>> Naveen Kumar <(E-Mail Removed)> writes:
>>>> 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.

>> That's not true. You missed one other.
>>
>> --
>> Lew

>
> Which is the other one ?


The Short which is referenced within the Card.

--
Lew
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
am I eligible for a MCP? TL MCSE 4 10-08-2007 08:34 PM
Collection problems (create Collection object, add data to collection, bind collection to datagrid) Řyvind Isaksen ASP .Net 1 05-18-2007 09:24 AM
Am I eligible for MCPD or any other certification by microsoft? chitreshkakwani@gmail.com MCSD 1 04-02-2007 08:11 PM
Is a B.E. curent tudent eligible? gagandeepbajaj@hotmail.com MCSD 14 01-05-2004 07:16 PM



Advertisments