Search code examples
javagarbage-collectiongarbage

Garbage Created By Object that is Never Referenced


Is any garbage created by an object that is never referenced?

The example I am thinking of is using a static factory method to create an object then having that object perform a function but never creating a reference to it.

For Example:

LoggerFactory.getLogger(Foo.class).info("logging some stuff");

Does this just create an unreferenced object in eden space that will be garbage collected as soon as the next collection happens?


Solution

  • getLogger returns an instance - whether it creates a new one or returns a previously cached one is up to the LoggerFactory's implementation. If this object is no longer referenced from inside the factory in some way, it would be eligible for garbage collection.