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 Doubt regarding Garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubt regarding Garbage Collection" Watch "Doubt regarding Garbage Collection" New topic
Author

Doubt regarding Garbage Collection

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Hi

Have a look at the code:



It is a part of the self test exercise in "Assignments" chapter in the Kathy-Bert SCJP for Java 6 book. Question is how many objects are available for GC when //do stuff line is reached. Options are:

A. 0
B. 1
C. 2
D. Compilation fails
E. It is not possible to know
F. An exception is thrown at runtime

Answer is C. Explanation they give is that only c1 and its contained Short object are available for GC.

My understanding: When we pass c2 reference as argument while invoking the method go(CardBoard cb), it means that we are actually passing a copy of the bits that refer to the object referenced by c2 created earlier. Correct? So when we are setting the passed reference to null inside the go() method, shouldn't the object become available for GC too in addition to c1?


~ Mansukh
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Got it. Lazy thinking. Closing the post.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37907
    
  22
Not at all. Please explain why you thought the reference passed to line 9 is set to null.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Campbell Ritchie wrote:Not at all. Please explain why you thought the reference passed to line 9 is set to null.


Got it Ritchie. The copy of the reference is set to null, not c2 itself.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37907
    
  22
Let’s have a bit more explanation, and the official terms. A lot of people get that sort of thing wrong, but there is a name for it.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

I did not get you Ritchie. Do you want some sort of explanation from me?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37907
    
  22
I was hoping you would go on about pass by reference.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

There is no pass by reference in Java first of all. It is always pass by value, whether it be primitives or reference variables.

a) In case of primitives, the copy of bits which point to the primitive value is passed. So , when we manipulate the variable inside a method, the original primitive remains unchanged.

b) But in case of reference variables being passed as arguments to methods, the copy of bits pointing to the object is passed. If we change an instance variable value, it will be reflected in the original object's state.

Is this what you were looking for?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37907
    
  22
Mansukhdeep Thind wrote:There is no pass by reference in Java first of all. . . .
I know that, you know that, but some people reading the book might not know that. And you find websites like this one (see no 6) which get it completely wrong.
b) But in case of reference variables being passed as arguments to methods, the copy of bits pointing to the object is passed. . . .

Is this what you were looking for?
Yes. A copy of information to find the address of the object is passed. As you say, if the object is mutable, a method can change its state.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Just to understand then. What exactly is pass by reference Ritchie? Can you educate me regarding that?
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 219

Mansukhdeep Thind wrote:Just to understand then. What exactly is pass by reference ?


hiee Mansukhdeep- I just searched and got a nice "Campfire story". look into this. PassByReference.

HIH


OCPJP
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 219

I guess bit-patterns are copied when a reference variable is passed.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37907
    
  22
In pass by reference, you pass the address of the location of the data such that the contents of the location can be changed. If you pass a reference type by value, however, the memory location contains the same object as before and you cannot change that.

Mutable reference types, however, can be manipulated and their internal state changed. Read about cats and how they naturally pass themselves around the neighbourhood, exclusively by value.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Ishan Pandya wrote:I guess bit-patterns are copied when a reference variable is passed.


It would be hard to imagine a parameter passing scheme where bit patterns aren't copied.
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 219

Jeff Verdegan wrote:
It would be hard to imagine a parameter passing scheme where bit patterns aren't copied.


like how is that possible jeff?? then what actually is passed in parameter passing scheme.?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Ishan Pandya wrote:
Jeff Verdegan wrote:
It would be hard to imagine a parameter passing scheme where bit patterns aren't copied.


like how is that possible jeff?? then what actually is passed in parameter passing scheme.?


Well, for one thing, it's always bits. That's sort of obvious, I'd think, since everything you store or move or operate on in a computer is a pattern of bits, yes?

Beyond that, in Java, what's passed is always the value of the parameter. So for primitives it's the int value or double value or whatever, and for references it's the reference value. The caller's value of the parameter is copied into the method's local variable. Note that the value is never an object. The value of a variable (or any expression) in Java can only be a primitive, a reference, or null, never an object.

If your next question is "What is the actual reference value?" the answer is, we don't know and we don't care, because it's not specified, and each implementation is free to do it however it wants, as long as the end result behaves as required by the spec. We can think of the value as the "address" of the object, but it doesn't really have to be that, and that term isn't even really defined in the Java language. What it amounts to is "some value that the JVM will use to access the object we expect."

You can find more details on parameter passing mechanisms in the general CS sense with a web search.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Doubt regarding Garbage Collection
 
Similar Threads
Bug test chapter 3, question 1?
GC help
how many objects are eligible for GC?
Garbage Collection
GC question