GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Object reference and object and 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 "Object reference and object and garbage collection" Watch "Object reference and object and garbage collection" New topic
Author

Object reference and object and garbage collection

anuj ojha
Greenhorn

Joined: Feb 11, 2011
Posts: 2
Hello All,
I am trying to understand object reference and garbage collection. Here is what confuses me.

1) what is difference between object and object reference?


Here I think that mybox is object of class Box. Now what is object reference?

2) garbage collection states "when there is no reference to object the memory occupied by that object is freed.

Now how does one know when there is no reference to object?

Please help.

Thanks in advance.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61315
    
  66

anuj ojha wrote:Here I think that mybox is object of class Box. Now what is object reference?

You think incorrectly. Unlike languages like C and C++, variables in Java cannot be objects, they can only reference objects. So mybox is a reference to a Box, not a Box.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

Answer to your second question.

There are a couple of different algorithms that the GC can use to find out what to clean out. Some of them are more aggressive in keeping the memory usage as low as possible and others are trying disturb the program as little as possible. But they all work more or less in the same way.

The GC goes through all the object references and marks objects that it thinks could be cleaned out. It also searches for islands of object references, such as an ArrayList that has no reference to it. When it has found an object an marked it X number of times, it sweeps it away.

If you google for 'java garbage collection tutorial' then you can spend a couple of hours reading about different algorithms and how they work. If you master this, you don't have to do a lot of coding, since you will be the "tuning master" ;)
anuj ojha
Greenhorn

Joined: Feb 11, 2011
Posts: 2
@Bear Bibeault Thanks,
@ Ove Lindström Yaa I will try to find such tutorials and study. Thanks though.
swapnil kataria
Ranch Hand

Joined: Feb 26, 2011
Posts: 64
Box mybox = new Box();

in your example mybox is not object, it is reference of the object which is created by (new Box();)
means mybox is holding bits internally which point to the object,

and gc is the process of cleaning the object from memory which are not used longer,
e.g.
String s1 ="Abc"
s1 = "xyz;

after line 2 the object which is created internally as new String("Abc")
will not have any live reference so it will available for garbage collection, because s1 is now holding the object new String("xyz")

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39088
    
  23
I think you may be mistaken about garbage collection for Strings; I think (but I am not certain) that String literals are "live" as long as the classes they are referred to from are "live". Do a search for "Strings, literally" and you will find a useful JavaRanch Journal article which will probably explain that to you.
 
GeeCON Prague 2014
 
subject: Object reference and object and garbage collection