-1

I have to make class, who's object is never garbage collected . So If I give class as an static does it will avoid garbage collection on his objects,Or is ther any another way to do that ??

klobin
  • 41
  • 1
  • 11

5 Answers5

4

Short answer : No. Marking your class static may NOT save it from garbage collection

Making a class static may avoid garbage collection for the particular class loader which was responsible for loading it. However, if this classloader gets opted for garbage collection, then all classes loaded via it ( static or non-static ) will also be garbage collected.

Usually this is the case when you have multiple class loaders in your application.

Other than that, an object ( any object ) is opted for garbage collection when it becomes unreachable

From the JLS (source)

A class or interface may be unloaded if and only if its defining class loader may be reclaimed by the garbage collector [...] Classes and interfaces loaded by the bootstrap loader may not be unloaded.

Tash Pemhiwa
  • 7,590
  • 4
  • 45
  • 49
Saif Asif
  • 5,516
  • 3
  • 31
  • 48
  • "Making a class static may avoid garbage collection for the particular class loader which was responsible for loading it." - How??? – maaartinus Jul 17 '14 at 10:30
  • Its only the case where there are more than one class-loaders. One loader may get 'unloaded' during flow of the application and thus any static classes that were loaded by this loader will also get freed up. So, the life of a static class will be the same as the life of the class-loader – Saif Asif Jul 17 '14 at 10:57
  • But there's no `static class` in this sense... The static modifier means nothing but that the *instances* don't get an implicit pointer to the enclosing class (so static classes are just like top level ones in this respect). All classes reference their classloaders and vice versa; there's no difference between the three classes in `class A { static class B {} class C {} }`. – maaartinus Jul 17 '14 at 12:54
  • `But there's no static class in this sense` which sense ? – Saif Asif Jul 17 '14 at 18:30
  • In a sense similar to `static` fields, which is probably what the OP was thinking about. Actually, in any sense making the `Class` object behave differently w.r.t. the GC. – maaartinus Jul 17 '14 at 18:45
1

Static variables are not reclaimed by the garbage collector when the corresponding static variable class is loaded into the JVM.

Please refer to below stackoverflow links

Community
  • 1
  • 1
pranabh
  • 26
  • 3
0

An object is either reachable or unreachable, and this has nothing whatsoever to do with which class it is an instance of.

If your object is unreachable, it is garbage because you have no way to ever contact it.

If you want to make your object live on throughout the application's lifetime, simply assign it to a static variable so that it stays reachable forever. Basically, you are looking for the Singleton pattern.

The static modifier, applied to a class, is only allowed for nested classes and it makes that class a non-inner class, which means it does not need an enclosing instance. This has nothing to do with garbage collection concerns.

William F. Jameson
  • 1,833
  • 9
  • 14
0

If I declare a class as an static, will that prevent garbage collection of its instances?

No.

The static modifier on a (nested) class means something rather different to static on a field. In particular, it has (almost1) nothing to do with reachability or garbage collection.

Is there any another way to do this?

If you want to make sure that an instance of some class is never garbage collected, you must make sure that it is always reachable.

One approach is to declare a static variable and assign a reference to your instance to that variable. Under normal circumstances2, the static variables remain reachable for an entire application run, and hence the objects that they reference remain reachable. That means they won't be garbage collected.

Another approach would be to hold the reference in a local variable declared in your main(...) method.

And there are other ways too.


1 - The slight gotcha is that an instance of a non-static nested class has an implicit reference to an instance of the enclosing class. But that affects the reachability of the latter instance, not the former instance.

2 - There are circumstances in which a classes statics do get garbage collected, but involves creating classloaders and dynamic loading. In fact, a more common problem with this scenario is classes / instances that don't get garbage collected.

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

If you want your class to not be garbage collected you have to keep it referenced. The best way to do it is to have your loader class (Application in Android for example) hold a reference to it. As long as Application is alive you can be sure that your class won't be touched by the garbage collector. Make sure it's a strong reference instead of a weak reference or even phantom reference

Martin Nowosad
  • 791
  • 8
  • 15