• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Object reference and object and garbage collection

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 67756
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 326
Android Mac OS X Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Bear Bibeault Thanks,
@ Ove Lindström Yaa I will try to find such tutorials and study. Thanks though.
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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")

 
Marshal
Posts: 80874
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic