I have a memory leak that I can't figure out (it's been a while since I've done stuff in java...)Basically I have a JFrame that takes some basic user input to get the parameters for a simulation. This main JFrame then passes the input to another class which runs and displays the results. Whenever I close the second JFrame the resources aren't freed and after multiple runs I get a out of memory/heap error. The setup is below:public class MainWindow extends JFrame { ..take some input private void buttonActionPerformed(java.awt.event.ActionEvent evt) { Results r = new Results(parameters); }}public class Results extends JFrame { public Results(parameters) { ...use parameters and start sim (which creates a bunch of new objects) ...display results in this JFrame setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); }}The MainWindow stays up so you can change the parameters and rerun. Each time you hit go a new Results window is created. I keep running out of heap space though after hitting go a few times, but closing the corresponding window (i.e start sim -> close results frame -> start sim -> close results frame -> start sim -> crash). Anyone have a guess why the resources in Results frame aren't being freed?[Edited on February 4, 2012 at 7:58 PM. Reason : ]
2/4/2012 7:58:04 PM
What's your JRE version?
2/4/2012 8:13:12 PM
1.6
2/4/2012 8:46:22 PM
why not initialize Results r as a class variable, rather than instanced in the method?
2/4/2012 9:01:19 PM
I tried that (and a lot of other permutations) and it still happens. I also tried overriding dispose in Results to null everything out, call System.gc(), and then call super.dispose(). I can't figure it out.
2/4/2012 9:04:27 PM
Are you able to create multiple results windows simultaneously?Could be it for some reason Java thinks "r" is still referenced from the MainWindow and keeps it around, even after multiple window spawns?
2/4/2012 9:45:32 PM
Yeah, every time I hit the button a new window pops up, like it should. Hitting the button twice without closing the popups = hitting the button once, closing the popup and hitting it again in terms of memory usage. The simulation creates really big 2D arrays of doubles so there must be a reference to one of them floating around in my main GUI.[Edited on February 4, 2012 at 11:03 PM. Reason : ]
2/4/2012 11:03:07 PM
I think I figured it out for anyone who might be interested. In my results viewer I'm using a scientific computation package called "Jhepwork" for plotting. Each of the plots are added to my Results frame via a getContentPane() method, which returns a JFrame. Well, for whatever reason these were not getting disposed of properly when I closed each Results window. I ended up overriding dipose() in my Results window and explicitly calling the proper dispose methods there before calling super.dispose(). Anyway, lesson learned. Keep track of any ContentPanes you may have used.[Edited on February 5, 2012 at 10:26 AM. Reason : ]
2/5/2012 10:25:35 AM
i thought there was a command you could run to clean up to prevent things like this?
2/5/2012 2:14:33 PM
sounds like you found it, but for future reference I've used this eclipse plugin in the past to track down leaks: http://www.eclipse.org/mat/ its pretty good especially when you can reliably reproduce the leak
2/5/2012 10:05:59 PM