[ajug-members] Swing and garbage collection
Joe Sam Shirah
jshirah at attglobal.net
Wed Jun 30 02:17:38 EDT 2004
Hi John,
I think we'd need to know more details ( possibly too many for a mailing
list ) to give an application appropriate answer, but here are a couple of
raw responses.
> I tried causing it to extend JFrame and calling objectname.hide(),
> but no luck, still garbage collected.
The AWT thread is what keeps Swing ( and AWT, naturally ) apps alive.
The AWT thread is not started and connected to the app until the components
are realized. From your description above, I don't think that ever happens,
so the main thread ends. Just to keep things confusing, in some versions
and on some systems, main stays alive anyway, but obviously you can't count
on that behavior.
It does seem to me that trying to make MainApp a Swing participant could
raise a number of new issues and is probably not the way to go.
The main thread is a thread like almost any other and threads die a
natural death at the end of the thread code. So something you could do is
put in a Thread.sleep() loop that doesn't end until a check variable matches
an end condition. Notice that's nothing to do with Swing, just mostly
standard thread handling.
It does seem like an odd way to do things though. Normally your
description brings the Singleton pattern to mind. But, without an
understanding of your app, I don't have a good place to go from there. HTH,
Joe Sam
Joe Sam Shirah - http://www.conceptgo.com
conceptGO - Consulting/Development/Outsourcing
Java Filter Forum: http://www.ibm.com/developerworks/java/
Just the JDBC FAQs: http://www.jguru.com/faq/JDBC
Going International? http://www.jguru.com/faq/I18N
Que Java400? http://www.jguru.com/faq/Java400
----- Original Message -----
From: "John Wells" <jb at sourceillustrated.com>
To: <ajug-members at ajug.org>
Sent: Tuesday, June 29, 2004 11:13 PM
Subject: [ajug-members] Swing and garbage collection
> I have a swing application that is started by running a class called
> MainApp. The class has a main method that creates an object of itself,
> creates a number of different JFrames and populates a few of its own
> fields with various objects. It's never meant to be seen...more of a
> common entry way into the application.
>
> The problem is that I'd like it to remain in scope, so that the fields
> that it instantiates are accessible for the entire lifetime of the
> application. This is because it creates a few common resources ( a system
> tray icon if it's run on Windows, for example).
>
> What's the accepted standard for persisting things like this in swing?
> What is this even called, so that I might google efficiently? Currently,
> as soon as the execution thread has passed through it, it's gone. I tried
> causing it to extend JFrame and calling objectname.hide(), but no luck,
> still garbage collected.
>
> Thanks for the help!
>
> John
>
More information about the ajug-members
mailing list