Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

garbage collection

 
priya rishi
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After line 6, how many objects are eligible for garbage collection?
my answer is 1.is it right?

class C
{
public static void main(String a[])
{
C c1=new C();
C c2=m1(c1);
C c3=new C();
c2=c3; //6
anothermethod();
}
static C m1(C ob1){
ob1 =new C();
return ob1;
}
}
 
Henry Wong
author
Marshal
Pie
Posts: 21195
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please Quote Your Sources.

Thanks,
Henry
 
Campbell Ritchie
Sheriff
Pie
Posts: 49402
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain why you think there will be one object eligible for GC.
I suspect it is correct, but we would like to see what you think.

And please find the code button; it makes your code easier to read.
[ October 30, 2008: Message edited by: Campbell Ritchie ]
 
priya rishi
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my source is online dump.
i am studying garbage collection for the first time.so i want to understand it well.

1. C c1=new C();
object is created with ref variable c1.

2.C c2=m1(c1);
when method m1(c1) is called another object is created with ref variable ob1.
ob1 =new C();


3.return ob1;
as the object ob1 is returned to the called method, it is not eligible for garbage collecion.
c2 will now have reference to the same object with reference ob1.

4.C c3=new C();
object with ref variable c3 is created.

5.c2=c3;
but now reference c3 is passed to c2 , that means object with c2 reference no longer has reachable reference,that means it is available for garbage collection.


one object is available for garbage collection.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12146
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Online Dump" is no more quoting a source than saying "from some book somehwere".
 
Campbell Ritchie
Sheriff
Pie
Posts: 49402
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by priya rishi:
my source is online dump.
. . .
one object is available for garbage collection.
Are you sure it's one? I am not convinced any more.

Of course, I learned Java the easy way, for a degree. So I never had to answer this sort of question to pass

And, as Fred said, please quote a proper source.
 
Meet Gaurav
Ranch Hand
Posts: 492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class C
{
public static void main(String a[])
{
C c1=new C();
C c2=m1(c1); // One Object 1st line Object
C c3=new C();
c2=c3; //6 // One Object c2 Object
anothermethod();
}
static C m1(C ob1){
ob1 =new C();
return ob1;
}
}

2 Objects are available. I guess if it is wrong. Please coorect me
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Meet Gaurav:
class C
{
public static void main(String a[])
{
C c1=new C();
C c2=m1(c1); // One Object 1st line Object
C c3=new C();
c2=c3; //6 // One Object c2 Object
anothermethod();
}
static C m1(C ob1){
ob1 =new C();
return ob1;
}
}

2 Objects are available. I guess if it is wrong. Please coorect me


I agree with priya. Which objects do you think can be GCed ?
 
priya rishi
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the website:

http://www.examsguide.com/scjp/freequestions2.html
(question no.13)

the ans given is 2

but i think its one.
so only i am not sure ,whether its 1 or 2.
Thanks for all your support.
 
Meet Gaurav
Ranch Hand
Posts: 492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any way, after calling C c2=m1(c1); this will again create a new object. At the time old new c() is also eligible rite ??
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Meet Gaurav:
Any way, after calling C c2=m1(c1); this will again create a new object. At the time old new c() is also eligible rite ??


A new C is created in the method and c2 is set to reference it. I'm not sure what you mean by 'old new C()'. The only previously created object is the one that c1 references.
 
Matteo Di Furia
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In method m1, a new object is created and assigned to the reference you pass as an argument to m1. Hence, when you execute



in m1 a new object is created and assigned to c1, then the same object is returned and assigned to c2. This way, also the object previously referenced by c1 is eligible for GC.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Matteo Di Furia:

in m1 a new object is created and assigned to c1, then the same object is returned and assigned to c2. This way, also the object previously referenced by c1 is eligible for GC.


First, it isn't true that there is an object assigned to c1 in m1. Second, if it was true, the answer still would be "1". Can you tell why?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12146
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


execution goes like this:

on line 3, we create a C object (call it object1), and assign the reference c1 to point to it.

in line 4, we pass the reference to the method m1, and go to line 7.

on line 7, we assign the reference to a newly created C object (call it object2). note that c1 in the main block still points to object1 - it is not changed by the method. we return the reference ob1 (NOT object1) to the main method, thus having c2 now point to object2. c1 is still pointing to object 1.

on line 5, we create a new object (object3), and assign the reference c3 to point to it.

on line 6, we assign reference c2 to point to the same object as cs (object3). This leaves nothing pointing to obj2, so that is eligible for GC.

Since c1 still points to object1, and c2 and c3 both point to object3, they are not eligible.

1 object.
 
Matteo Di Furia
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mmm, I did some test and I admit I'm wrong. But then I don't get a thing.
Why if you pass an object to a method and in the method you modify the object, the caller will see the modified version of the object (I know this does NOT apply to primitive types), but if you pass the same object and you modify the reference, caller will see the original value and not the new one ?

Other than this, I also must admit I usually tend to think that if there's someone wrong between me and some author I'm reading a book/tutorial of, that one is most certainly me. In this case too, if I were reading that page I would have tried to find 2 eligible objects (like I did in my reply) just giving for sure that the page was right. (yes, you can call me moron !)
 
fred rosenberger
lowercase baba
Bartender
Posts: 12146
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
personally, i think of references as notecards with address on them, and they refer to a house.

when you 'pass an object' into a method, you are really passing a copy of the reference. So, you are effectively passing a copy of the address.

when you make this call:

m1(c1);

and go into the m1 method:

static C m1(C ob1){

ob1 now has a copy of the address of the object that was referred to by c1. effectively, there are now two note cards with the same address written down on them. if you then say:

ob1 =new C();

you are erasing the old address of the ob1 card, and writing a new house's address on it. this in no way effects what is written on that c1 card.

HOWEVER, if in the method, you called some method that mutates the object, say ob1.paintMasterBedroom("red"), then the workers will go to the house at the address, and paint the bedroom. Here it doesn't matter if they got the address from the ob1 card or the c1 card... the bedroom in that house is going to be painted.
 
priya rishi
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everybody, especially fred rosenberger for your clear explanation.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic