Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #30438
| Newsgroups | comp.lang.java.programmer |
|---|---|
| Date | 2014-11-28 13:29 -0800 |
| References | <c912d4cc-a1dc-4901-85a5-a6e800bb487c@googlegroups.com> <m4nqf9$731$1@dont-email.me> <a95c8082-2a14-4dd3-a125-ad8f7d316b7a@googlegroups.com> <m4o40l$g06$1@dont-email.me> |
| Message-ID | <f8dc3fea-1129-4acd-b3f1-dad9ec3ac5ac@googlegroups.com> (permalink) |
| Subject | Re: heap memory issue, related with garbage collection |
| From | John <xsli2@yahoo.com> |
I solved my problem.
Here is my original code:
private void displayPic(final int picCount)
{
String pngFileNameWithPath = xxx; //get PNG file name based on int picCount
final ImageIcon imageIcon = new ImageIcon(pngFileNameWithPath);
final JLabel picLabel = new JLabel();
picLabel.setIcon(imageIcon);
final JPanel picPanel = new JPanel();
picPanel.add(picLabel);
_jPanel.add(picPanel, BorderLayout.CENTER);
_jFrame.getContentPane().add(_jPanel);
_jFrame.setTitle(pngFileNameWithPath);
_jFrame.setVisible(true);
}
The problem is when this method was called 150 times, then heap memory ran out. I found the cause is that at the end of the method, even though those references('picLabel', 'picLabel' etc) are out of scope and dead, the objects that once were referenced by them were not garbage collected and that was due to that those objects, even though not referenced by 'picLabel', 'picLabel' etc, were still referenced by some other references. In order to make those objects really collected by JVM garbage collection, I added these code near the end of the method:
picLabel.removeAll();
picPanel.removeAll();
_jPanel.remove(picPanel);
_jFrame.getContentPane().remove(_jPanel);
I am not sure if they all are needed or if one is covering another. But anyway, I can keep all references inside the method(no need to make them class instance variables) and no more memory issue.
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
heap memory issue, related with garbage collection John <xsli2@yahoo.com> - 2014-11-21 08:41 -0800
Re: heap memory issue, related with garbage collection Tassilo Horn <tsdh@gnu.org> - 2014-11-21 17:47 +0100
Re: heap memory issue, related with garbage collection Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-11-21 11:49 -0500
Re: heap memory issue, related with garbage collection John <xsli2@yahoo.com> - 2014-11-21 10:49 -0800
Re: heap memory issue, related with garbage collection Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-11-21 14:32 -0500
Re: heap memory issue, related with garbage collection John <xsli2@yahoo.com> - 2014-11-28 13:29 -0800
Re: heap memory issue, related with garbage collection Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-11-28 17:20 -0500
csiph-web