 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Araxes Tharsis Guest
|
Posted: Mon Dec 22, 2003 2:29 pm Post subject: Simple GC question |
|
|
When I create an object only with:
new MyJFrame();
when will this object be Garbage Collected, as I have no explicit reference
to it?!
This is what is generally done in Swing, and I don't understand very well
what goes on with GC (the object is not gc because the frame stays visible
indefinitely...).
Thank you very much,
Araxes Tharsis
|
|
| Back to top |
|
 |
Eric Sosman Guest
|
Posted: Mon Dec 22, 2003 6:24 pm Post subject: Re: Simple GC question |
|
|
Araxes Tharsis wrote:
| Quote: |
When I create an object only with:
new MyJFrame();
when will this object be Garbage Collected, as I have no explicit reference
to it?!
|
The object becomes eligible for collection immediately,
but there is no guarantee of exactly when the collection will
occur. There is not even a guarantee that collection will occur
at all: if your program never runs short of memory, the JVM may
simply allow the garbage to hang around indefinitely.
| Quote: | This is what is generally done in Swing, and I don't understand very well
what goes on with GC (the object is not gc because the frame stays visible
indefinitely...).
|
Please provide more detail on what you think "is generally
done in Swing." As far as I know, the JFrame will not become
visible until you call its setVisible() method -- for which you
need to have a reference to the JFrame. Once the JFrame has
been made visible, Swing keeps its own internal references to
it even if you discard your own.
--
[email]Eric.Sosman (AT) sun (DOT) com[/email]
|
|
| Back to top |
|
 |
Mark Haase Guest
|
Posted: Sun Jan 11, 2004 1:46 am Post subject: Re: Simple GC question |
|
|
In article <3fe700ec$0$8813$a729d347 (AT) news (DOT) telepac.pt>,
"Araxes Tharsis" <pnrmaia (AT) hotmail (DOT) com> wrote:
| Quote: | When I create an object only with:
new MyJFrame();
when will this object be Garbage Collected, as I have no explicit reference
to it?!
This is what is generally done in Swing, and I don't understand very well
what goes on with GC (the object is not gc because the frame stays visible
indefinitely...).
Thank you very much,
Araxes Tharsis
|
The GC will throw it away when no other objects reference it.
So if you say
new MyJFrame();
then yes, this will get GC'ed quickly, but who cares? You don't even
have a reference to it, so its useless to you.
If you passed it to another object however, ie.
add(new MyJFrame());
then presumably that object will keep a reference to it, in which case
it won't get GC'ed.
--
| Quote: | /| /| |2 |
mehaase(at)sas(dot)upenn(dot)edu |
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|