I want to integrate the weak, soft and phantom classes into my code. But before I do, I want to understand if or not the api objects themselves, that is, an instance of a Reference object take up a lot of memory.
When is ws garbage collected? It seems like it will always have a strong reference to itself.
Would creating multiple such reference objects actually burden my code instead of making it more efficient?
Seems to me you're accusing the people who designed that class of being idiots. But they aren't. Those classes actually do work the way the documentation says they do.
But anyway let's try to answer your question. "When is ws garbage collected?" Well, it isn't. That's because "ws" is a variable and variables never get garbage collected. It's objects which are garbage collected. However, like everybody else, you're using the (confusing) short version for "When is the object referred to by ws garbage collected?" This is actually a good question.
Once the variable ceases to exist, then the strong reference to which you refer also ceases to exist. So when does it cease to exist? That depends. If the variable is a local variable, then once the flow of control leaves the variable's scope (the block in which it's declared) the object may be garbage collected. However if the variable is an instance variable of a class, then the variable ceases to exist only when the object of which it is part is garbage collected. (And if it's a static variable of a class, then it ceases to exist only when the class is garbage collected, which normally doesn't happen.)
So yeah: if you create a WeakReference, you will want to create a variable which refers to it (that strong reference) for as long as you need the WeakReference to exist. Just like that example. Otherwise you'll find your WeakReference objects vanishing before they should. Trust me, from personal experience, this isn't an easy thing to debug.
Basically the strategy is to have an instance variable in an object which refers to a WeakReference wrapping something -- a listener, let's say. Then you can pass around that WeakReference to other objects which need to interact with it. Once your object is garbage collected, all of those other objects find themselves with WeakReferences which point to nothing (after the garbage collection clears them, that is). The advantage of this design is that the object doesn't have to remember who it passed those WeakReferences to. If you used strong references, then the object would have to clean up memory by telling all those other objects to stop using the strong references.
Joined: Jan 16, 2009
Paul Clapham wrote:Seems to me you're accusing the people who designed that class of being idiots.
Paul, I don't know what you are on, but I don't know where in my post you felt I was accusing anyone.
Its obvious who the idiot really is. Don't ever try to interpret my words into something hateful. Looks like you are one of the moderators, great job at making this 'A friendly place for programming greenhorns'.
Go on, keep ticking people off but stay the **** out of my way. The next time, feel free to stay away from my questions. I can do without your arrogant, egotistic answers.