wood burning stoves 2.0*
The moose likes Java in General and the fly likes Reg garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Reg garbage collection" Watch "Reg garbage collection" New topic
Author

Reg garbage collection

Pradeep Kumar
Ranch Hand

Joined: Sep 11, 2007
Posts: 77
1. public class X {
2. public static void main(String [] args) {
3. X x = new X();
4. X x2 = m1(x);
5. X x4 = new X();
6. x2 = x4;
7. doComplexStuff();
8. }
9. static X m1(X mx) {
10. mx = new X();
11. return mx;
12. }
13. }

Since java uses pass by reference for objects the reference passed to the method m1() will refer to the sameinstance x declared at line 3. Inside the method we assign the reference to a new object thus removing the refernce to the previously pointing object. Can you tell me how many objects are eligible for garbage collection. In the book its given as the object created at line 4 is eligible for garbage collection but i think its line 3 onject that is eligible for garbag collection. can i gert a clarification
Thanks


SCJP 1.6
Pradeep Kumar
Ranch Hand

Joined: Sep 11, 2007
Posts: 77
This is my undersatndin to the above program

1.After line 3 this will happen
_____
| |
| |
| |----->x
| |
|_____|

2. After line 4 this will happen
_____
| |
| |
| |--//--->x
| | |
|_____| |
|
_____ |
| | |
| |---|
| |----->x2
| |
|_____|

3. After line 6 this will happen
_____
| |
| |
| |---//-->x
| | |
|_____| |
|
_____ |
| | |
| |---|
| |----->x2
| | |
|_____| | // Not furthur referenced
//
_______ |
| | |
| |----
| |----> x4
| |
|_____|

So as x2 and x4 both refer to same x4 boject and x doies not have a reference the object created at line 3 is eligible for garbage collection. Is my understanding correct.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Sorry, I don't understand your diagrams...

What object does x reference after line 4 is executed, in your opinion?


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
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Yes you are right Pradeep. Java does pass by reference only.

But inside the method m1, the received object is stored in a reference variable 'mx' and that object is local to that method. You are reassigning the local 'mx' variable to point to a new object and NOT the actual object being passed from your main() method.


...In the book its given as the object created at line 4 is eligible for garbage collection but i think its line 3 onject that is eligible for garbag collection. can i gert a clarification


Still, the object created in line 3 is being referred by the reference variable 'x'. Only the object being pointed by the reference variable 'x2' is lost since 'x2' is being made point to the same object as that of 'x4'.

Does that help Pradeep?


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3426
    
  12
Originally posted by Raghavan Muthu:
[QB]Yes you are right Pradeep. Java does pass by reference only.QB]


No !! Both you and Pradeep are wrong. Java does pass by value - always.
However, you have to remember that Java does not pass objects to methods. It passes references to objects.

Here, x is a reference to an X object. When you call m1, you are passing a copy of this reference to the method. The original x cannot be changed in m1, because only a copy of it was passed.


Joanne
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344


..No !! Both you and Pradeep are wrong. Java does pass by value - always.


It was an idiotic typo mistake. By thinking that i am confirming for 'pass by value' i typed the reverse.

Thank you Joanne Neal !
Pradeep Kumar
Ranch Hand

Joined: Sep 11, 2007
Posts: 77
But java uses call by reference when we pass objects to the method. i.e, when we pass an objec to a method it internally passes call by vlaue but the value is just a bit pattern and not the object. so the passed object will still refer to the object for ex

1 Object o = "toBePassed";

2 doStuff(o);

in the doStuff method

3 doStuff(Object obj){
4 Object o = obj;
}

the object obj refers to the same object created at line 1 also o created referenced at line 4 still points to the same object(created at line1) is this right?
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344


..the object obj refers to the same object created at line 1 also o created referenced at line 4 still points to the same object(created at line1) is this right?


Yes. the bit patterns to reach the object is NOT changed yet. so they are the same.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reg garbage collection
 
Similar Threads
Question on Garbage collection
Garbage Collection question
How Many Objects are eligible for garbage collection?
garbage collection
Garbage collection explanation