*
The moose likes Java in General and the fly likes Will this cause a memory leak? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Will this cause a memory leak?" Watch "Will this cause a memory leak?" New topic
Author

Will this cause a memory leak?

M Burke
Ranch Hand

Joined: Jun 25, 2004
Posts: 388
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

Joined: Sep 16, 2005
Posts: 1780
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.


There is no emoticon for what I am feeling!
M Burke
Ranch Hand

Joined: Jun 25, 2004
Posts: 388
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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
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


Tony Morris
Java Q&A (FAQ, Trivia)
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
M Burke
Ranch Hand

Joined: Jun 25, 2004
Posts: 388
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
Bartender

Joined: Oct 14, 2005
Posts: 18669
    
    8

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

Joined: Jul 11, 2001
Posts: 14112
As an aside, *scope* doesn't have anything to do with garbage collection, anyway. *Lifetime* of *variables* has.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Will this cause a memory leak?