Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are non-static inner class objects garbage collected after they are no longer referenced?

I have a single spring bean similar to the following:

public class MyServiceImpl {
    private MyDAO myDAO;

    public class MyInnerClass implements SomeInterface<MyInnerClass> {

        @Override
        public MyInnerClass loadFreshObject(final String key) {
            return myDAO.load(key);
        }
    }

}

Instances of MyInnerClass are being created in code outside of the spring bean but no reference to those instances are being kept.

Assuming I have no control over the use of these public non-static inner classes (I know ideally these would be private and static to avoid leaking the reference to 'this'), will the created instances of 'MyInnerClass' be correctly garbage collected?

I have run my own tests on this by overriding the finalize() and it appears that the instances are correctly being garbage collected, I was just hoping for clarification on this.

Thanks

like image 873
James Faulkner Avatar asked Feb 24 '12 10:02

James Faulkner


People also ask

Do static objects get garbage collected?

Static variables cannot be elected for garbage collection while the class is loaded. They can be collected when the respective class loader (that was responsible for loading this class) is itself collected for garbage.

When an object is no longer used which method is called automatically by the garbage collector in Java?

Finalization. Before an object gets garbage-collected, the garbage collector gives the object an opportunity to clean up after itself through a call to the object's finalize method. This process is known as finalization.

What is the difference between static inner class and non-static inner class?

Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class.

How does garbage collector remove unused references?

As long as an object is being referenced, the JVM considers it alive. Once an object is no longer referenced and therefore is not reachable by the application code, the garbage collector removes it and reclaims the unused memory.


2 Answers

Instances of the inner class will be garbage collected according to normal rules (i.e. when they are no longer referenced). However, each instance of the inner class contains a hidden reference to its parent instance of the outer class. This means that if there are any live references to instances of the inner class, they will prevent the associated instances of the outer class from being garbage collected. But it only works in that direction, not the other way around.

like image 129
Michał Kosmulski Avatar answered Sep 28 '22 12:09

Michał Kosmulski


Why wouldn't they garbage collected? The GC doesn't care about the type of an object. If it's unreachable, it's GCed. If it's reachable, it's not GCed.

like image 23
JB Nizet Avatar answered Sep 28 '22 11:09

JB Nizet