• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question about garbage collection  RSS feed

 
Dave Robbins
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

my app consists of a JSplitterPane with a JTree on the left
depending on user action I display one of several JPanels on the right
so let's say I have panels A and B which can be shown on the right
if A is showing and user actions cause B to be displayed, when is A destroyed by garbage collection?
how can I control this?
in some cases I'd like to ensure A doesn't get destroyed and maintains it's state until the user causes it to be displayed again

Thanks
Dave
 
Douglas Chorpita
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As long as a valid (non-null) reference to an object exists and can be accessed by a live thread, the object is not eligible for garbage collection.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"A" will never be destroyed as long as it's reachable in some way; in other words, if it's possible for you to display it again, then it won't be garbage-collected. You never have to worry about an object being destroyed "out from under you." If you have any way to refer to it, then it won't disappear.

If, on the other hand, "A" is removed from its parent and you don't have any variables that refer to it, then it may be garbage-collected whenever the JVM needs the memory back. It might be right away, or it might be a long time -- there's no saying.
 
Dave Robbins
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks

I see what's going on now
in my code it appeared to be getting destroyed when I stopped displaying it
I now realize it was just an unintended "feature" of my code :roll:

Dave
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!