V
V
Vladimir Yakushev2014-01-29 20:07:40
Java
Vladimir Yakushev, 2014-01-29 20:07:40

Can the garbage collector remove a Singleton?

There is a Singleton class that is responsible for getting the coordinates. This class has methods that start and disable the subscription to coordinate changes. Can the garbage collector remove a Singleton class from memory because it is not referenced in other active classes?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
K
kavdeev, 2014-01-31
@VYakushev

yes, maybe if there are no external references to the class or its instance, since the class is also an object, the
problem is solved by creating an external reference or by setting the jvm -Xnoclassgc

L
leventov, 2014-01-29
@leventov

I don't know of any way to implement a Singleton in Java that doesn't somehow use a static field with an instance of the class (at least not adequate ways). And a static field is always the root for the GC, whether it's private or not. So the answer is no, it can't.

A
Artyushov, 2014-01-29
@Artyushov

Firstly, if a class is loaded into a virtual machine, then the static field cannot disappear from it anywhere.
Secondly, why do you need to know about it at all? The situation when you need some data in the code, but it turned out to be collected by the GC, is impossible.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question