• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reference api

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
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.

For example:


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?
 
Gina vernon
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anyone?
 
Marshal
Posts: 28176
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Gina vernon
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Paul Clapham
Marshal
Posts: 28176
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I apologize for that. I didn't mean any such thing and I'm sorry I caused you to feel that way.

And the rest of the post? Hopefully you didn't let the first part prevent you from reading it?
 
Ranch Hand
Posts: 62
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Reference Api and the Garbage Collector
 
reply
    Bookmark Topic Watch Topic
  • New Topic