File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "garbage collection" Watch "garbage collection" New topic
Author

garbage collection

priya rishi
Ranch Hand

Joined: Oct 26, 2008
Posts: 119
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;
}
}


SCJP 5 , SCWCD 5
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

Please Quote Your Sources.

Thanks,
Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39079
    
  23
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

Joined: Oct 26, 2008
Posts: 119
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

Joined: Oct 02, 2003
Posts: 11354
    
  16

"Online Dump" is no more quoting a source than saying "from some book somehwere".


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39079
    
  23
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

Joined: Oct 08, 2008
Posts: 492
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

Joined: Aug 05, 2005
Posts: 3603
    
  15
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 ?


Joanne
priya rishi
Ranch Hand

Joined: Oct 26, 2008
Posts: 119
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

Joined: Oct 08, 2008
Posts: 492
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

Joined: Aug 05, 2005
Posts: 3603
    
  15
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

Joined: Jun 20, 2008
Posts: 102
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

Joined: Jul 11, 2001
Posts: 14112
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?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11354
    
  16



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

Joined: Jun 20, 2008
Posts: 102
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

Joined: Oct 02, 2003
Posts: 11354
    
  16

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

Joined: Oct 26, 2008
Posts: 119
Thanks everybody, especially fred rosenberger for your clear explanation.
 
GeeCON Prague 2014
 
subject: garbage collection