Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

does GC eat my JMenu?

Thread view: 
Martijn Mulder - 16 Dec 2005 08:57 GMT
I declare and define an instance of JMenu in the
constructor of my JFrame-class. Then I use
setJMenu(JMenu) to attach the menu to the
main window. It works well, I can use the
menu, I do not experience strange behavior.
But somehow I am concerned that the JMenu
no longer exists after program flow leaves the
constructor. Does the garbage collector remove
it and is it mere change that I have a menu, or
does the call to setJMenu keep the instance alive?

//class Instrument
public class Instrument extends javax.swing.JFrame
{

//constructor
public Instrument()
{

 //call parent constructor
 super(a);

 //menubar
 javax.swing.JMenuBar menubar=new javax.swing.JMenuBar();

//add items to menubar

 //set JMenuBar
 setJMenuBar(menubar);

}
}
Roedy Green - 16 Dec 2005 09:29 GMT
On Fri, 16 Dec 2005 09:57:42 +0100, "Martijn Mulder"
<mbmulder@wanadoo.nl> wrote, quoted or indirectly quoted someone who
said :

>I am concerned that the JMenu
>no longer exists after program flow leaves the
>constructor.

You have added the JMenu to the JFrame. the JFrame has a reference to
the JMenu.  Even after you drop all references to your JFrame, if it
is on screen Java still has a pointer to it.  Only after you dispose
the JFrame and drop all your own references to it will everything be
GCed.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Chris Smith - 16 Dec 2005 18:44 GMT
> I declare and define an instance of JMenu in the
> constructor of my JFrame-class. Then I use
[quoted text clipped - 6 lines]
> it and is it mere change that I have a menu, or
> does the call to setJMenu keep the instance alive?

Roedy explained the mechanism.  However, the more important response is
this.  Trust the garbage collector not to get rid of anything you are
still using.  Unless you use the java.lang.ref package, it is NEVER
possible for the garbage collector to dispose of an object that you
might be using for any purpose.  If you care whether the object exists
or not, then the answer is that the garbage collector won't dispose of
it.  Any time spent worrying about whether the garbage collector will
collect an object that you need to remain in existence is wasted time.

(Incidentally, there are a few gotchas in the other direction -- the
garbage collector keeping something that you don't need.  These tend to
result from programming sloppiness, or the use of classloaders other
than the system one.)

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Roedy Green - 17 Dec 2005 03:12 GMT
>(Incidentally, there are a few gotchas in the other direction -- the
>garbage collector keeping something that you don't need.  These tend to
>result from programming sloppiness, or the use of classloaders other
>than the system one.)

see http://mindprod.com/jgloss/packratting.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.