3

I'm just thinking about a way of keeping away Java objects from garbage collection even if it is not being referred for a reasonable amount of time.

How to do that?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 8
    if you are *not* referring these objects - whats your interest in keeping memory occupied ? – Bhaskar Oct 18 '11 at 12:03
  • 3
    Sounds like something dodgy is going on :) – Chris Dennett Oct 18 '11 at 12:04
  • why do you want the objects to remain alive when you have no reference to them (they will be impossible to retrieve to do anything with them) – ratchet freak Oct 18 '11 at 12:04
  • maybe OP' testing some memory leak detection tool :D – Rekin Oct 18 '11 at 12:09
  • If an object is not referenced, how will you find it again? – finnw Oct 18 '11 at 12:09
  • A very similar question was posted [here](http://stackoverflow.com/questions/1329926/how-to-prevent-an-object-from-getting-garbage-collected) – Barry Jordan Oct 18 '11 at 12:13
  • I'd be interested to hear what your use case is for this. Usually avoiding the garbage collector is a bad thing, but I've learned to never say never in development. Rules should never be broken until they need to be broken. ;-) – erturne Oct 18 '11 at 12:17
  • There is a case for this when it comes to a service. You might have a class that uses JNotify to monitor a file system and fire event on a bus when files are observed. In this case nothing would need a reference to the service class but it would need to be maintained for the life of the JVM. Granted, you could make a case that the JNotify system has a reference to the object. But same statement could be made of a timer that fires a monitoring event. – John B Oct 18 '11 at 12:18

5 Answers5

9

Have a static container in your main class that you put a reference to the objects in. It can be a Map, List, whatever. Then you'll always have a reference to the object, and it won't be reclaimed. (Why you would want to do this is another question...)

Which is to say: As long as a reachable reference to an object exists, it will not be garbage-collected. If your code has a reference and tries to use it, the object will be there. You don't have to do anything special to make that happen (nor should you). (A reachable reference means that the reference is from something that is, itself, reachable from something other than the things it references. Put more simply: The GC understands about circular references and so can clean up A and B even if they refer to each other, as long as nothing else refers to either of them.)

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
7

[...] even if it is not being referred for a reasonable amount of time.

If there's any chance what so ever that an object will be accessed in the future, the object will not be garbage collected.

This is due to the fact that if you have a reference to the object, it won't be garbage collected, and if you don't have a reference to the object, there's no way you will be able to access it ever.

In other words, an ordinary reference will never mystically turn into a null just because the garbage collector observed that the object hadn't been accessed for a long time and thought it was time to reclaim it.

aioobe
  • 413,195
  • 112
  • 811
  • 826
2

You could also create a static instance of the object in its own class. For example if it is a singleton, having a static instance field in the class.

John B
  • 32,493
  • 6
  • 77
  • 98
2

There are mechanisms that will hold a reference to an object, but still allow it to be garbage collected, if there are no other references otherwise.

Look at WeakReference and SoftReference. If you want more details on reachability as far as the jvm is concerned, see:

http://download.oracle.com/javase/6/docs/api/java/lang/ref/package-summary.html#reachability

As far as time is concerned, the garbage collector doesn't know or care about how often an object is used. Either another object has a reference to the target (even if it's not using it), or there are no references to the target. If there are no references to the object, it could never be used again, and will eventually be freed (even if you wanted to, you couldn't obtain a reference to the object again) The longer-living an object is, the longer it takes for the jvm to free it, due to generational garbage collection.

Austen Holmes
  • 1,889
  • 14
  • 9
1

I'm just thinking about a way of keeping away Java objects from garbage collection even if it is not being referred for a reasonable amount of time.

On the face of it, this question doesn't make sense. If an object is not referenced (or more a precisely, if it is not reachable) then the garbage collector will collect it. If you want to prevent an object from being garbage collected then you have to make sure that it is reachable. (Actually, it has to be strongly reachable to guarantee that it won't be GC'ed : see @Austen Holmes answer and the page that he references.)

But actually, I think that you are confusing "refered" / referenced / reachable with accessed or used; i.e. with the act of accessing a field or call a method of the object. If that is what you are asking, then I can assure that the garbage collector neither knows or cares whether your code has recently accessed / used an object.

The reachability criteria is actually about whether your code could access the object at some point in the future, and (therefore) whether the object needs to be kept so that this will work. The reachability rule means that if an object could be accessed, then it will be kept. It makes no difference how long it was since you last accessed it.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216