does GC eat my JMenu?

M

Martijn Mulder

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);

}
}
 
R

Roedy Green

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.
 
C

Chris Smith

Martijn Mulder said:
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?

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.)

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,049
Members
48,769
Latest member
Clifft

Latest Threads

Top