• Post Reply Bookmark Topic Watch Topic
  • New Topic

Dumb question about GC in Swing

 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I have a very basic question :

Supposing I have the following code :


My simple doubt is : Once I'm creating a new object without assign its reference to a valid variable, why it's not immediate eligible for garbage collection ?

As far as I know, any object reference becomes eligible for garbage collection when no active thread can access such reference. So, in this case who is able to access the form window reference if I'm not setting its reference to a valid variable ?

Thanks,
Edisandro.
[ September 21, 2006: Message edited by: Edisandro Bessa ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No such thing as a dumb question!

Windows are attached to other things, and it's those references from other things which keep them from being collected. For example, as soon as its made visible, a Window always has a peer, a native operating-system counterpart which actually draws the Window on the screen; even if nothing else references your Window object, that peer object will. Of course, lots of other things usually do: for example, a button you create might hold a reference to your Window as its parent.
 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent Ernest !!! I got it !

Regarding the parent window reference held by a buttom, I truly know about that but what has confused me is that I thought even the button holding a reference to the main window, no reference to the buttom was held nowhere by a valid variable, so I thought I would fall in the "Island of isolation" situation (Very much discussed on SCJP forum).

IMO, the key point you mention above is THE responsible for prevent the main window to be garbage collected.

Thanks a lot for your prompt reply.

Edisandro.
[ September 21, 2006: Message edited by: Edisandro Bessa ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right, the button will only affect things if it's being held as a member variable by something besides the Window itself.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!