• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Will this cause a memory leak?

 
M Burke
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a Singleton Factory that returns a reference to an Vendor object.
Since the Singleton's reference is static,
will any object I create with it (getVendor()) not get destroyed when the calling program method exits?

/**
* @author mburke
*Singleton that returns a Vendor reference
*/
public final class VendorFactory {
private static final VendorFactory factory = new VendorFactory();

private VendorFactory(){}

public static VendorFactory getInstance(){
return factory;
}

/**
* @return Returns the vendor.
* Each thread gets its own reference.
*/
public synchronized IVendor getVendor() {
return new Vendor1();
}

}
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The fact that the Vendorfactory is a singleton has no impact on when Vendor objects become eligible for garbage collection. Perhaps you are thinking of the factory keeping a collection of references to the objects it creates.
 
M Burke
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I am thinking is since getVendor() is called by a static reference, it never goes out of scope. So if you create an object inside it the object never loses it's reference and cant be garbage collected. Or am I wrong?
 
Henry Wong
author
Marshal
Pie
Posts: 21021
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hard call. The definition of a memory leak is something that is not used anymore, forgotten, and hence, does not get reclaimed.

In this case, it is a singleton. There is supposed to be one and only one. And it is suppose to be the same one for the lifetime of the application.


I guess... If being the same singleton is not necessary. You can hold it in a weak reference. The object will not get reclaimed as long as some other object is using it.

When all other references are gone, then you only have the weak reference in the singleton. Then if enough time goes by, the garbage collector can reclaim it. The only issue is that the factory must now make an extra check to see if it has been reclaimed. If it has, it must instantiate a new one.

Henry
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is all trickery.
class X{static Object o = new Integer(7);} // does this leak memory?
Of course not, for *exactly* the same reason that your code sample doesn't. Sure, a static field exists for the life of the class (and therefore, for the life of the loader that loaded it), which might be a long time, but what's that got to do with leaking memory? The fluffy definitions that skirt the issue are contrived. This really is not a complex issue.

If you wish to leak memory, do it properly - at least do it provably indefinitely:
http://jqa.tmorris.net/GetQAndA.action?qids=38&showAnswers=true
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The

new Vendor1();

object that is created in your singleton doesn't get referenced by the singleton. The singleton doesn't keep it from being garbage collected at all.
 
M Burke
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Those are some interesting examples. How about this? A static method working as a factory returning object references. The method never goes out of scope, so what happens to the objects it creates? Will they go to the gc when the calling method goes out of scope?

public static IVendor getVendor() {
return new Vendor1();
}
 
Paul Clapham
Sheriff
Posts: 20986
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The fact that an object was created in method X is totally irrelevant to the garbage collector. All GC cares about is references to that object. In your example there are no references to the new Vendor1 object, so as far as we can tell from that code, it could be garbage-collected immediately. However if that object, the result of calling "IVendor.getVendor()", is assigned to some other variable, then it can't be garbage-collected at least until that other variable goes out of scope. But from the code you've posted we have no idea what happens to the reference, so it's impossible to say when it might be garbage-collected.

But the fact that it was created by that method definitely has nothing to do with it.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As an aside, *scope* doesn't have anything to do with garbage collection, anyway. *Lifetime* of *variables* has.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic