Java garbage collection in the dialog box
*When I try to create a button in JFrame, I now encounter a very strange Java GC problem. When I click the button, it will display jdialog that needs to process and display some images and needs nearly 200m memory But the problem is that when I close the dialog and reopen it, sometimes it causes Java lang.OutOfMemoryError. (not every time)
Trying to solve this problem, I simplified the problem and did some experiments, which made me more confused
The code I used in the experiment is as follows When I click a button in a frame, I allocate 160m memory for an integer array and display a dialog box, but if I close the dialog box and reopen it, outofmemoryerror will appear I adjust the code and the result is:
>If I don't create a dialog box and display it, there is no memory problem. > If I add a call to system The windowscloselistener of GC () to the dialog box, and there is no memory problem. > If I call System. in the run () method GC (), memory problems will occur
public class TestController { int[] tmp; class TDialog extends JDialog { public TDialog() { super(); this.setDefaultCloSEOperation(JDialog.DISPOSE_ON_CLOSE); // If I uncommment this code,OutOfMemoryError seems to dispear in this situation // But I'm sure it not a acceptable solution /* this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.out.println("windowsclose"); TDialog.this.dispose(); System.gc(); } }); */ } } TDialog dia; public void run() { // If I do System.gc() here,OutOfMemoryError still exist // System.gc(); tmp = new int[40000000]; for (int i = 0; i < tmp.length; i += 10) tmp[i] = new Random().nextInt(); dia = new TDialog(); dia.setVisible(true); } public static void main(String[] args) { EventQueue.invokelater(new Runnable() { @Override public void run() { final JFrame frame = new JFrame("test"); frame.setDefaultCloSEOperation(JFrame.DISPOSE_ON_CLOSE); frame.setLocationRelativeTo(null); frame.setSize(200,200); JButton button = new JButton("button"); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { TestController controller = new TestController(); controller.run(); controller = null; } }); frame.add(button); frame.setVisible(true); } }); } }
I have read many articles describing the working principle of Java GC I think if Java tries to allocate some space in the heap and there is not enough free space, Java will execute GC, and if the object cannot be accessed from the GC root through the "GC graph", one edge from u indicates that you have a reference to v. root is something in a thread work stack or a native resource. It is useless and qualified to be collected by Java GC
The problem now is that when I click the button and try to create an integer array, the integer array I created last time must be eligible for collection by Java GC So why does it cause errors
Apologize for such a long description... I didn't have much strategy when asking questions, so I just tried to make it clear
In addition, the parameter I use to start the JVM is "Java - xmx256m"
resolvent
Solution
You previously assigned a new int [40000000], while TMP still retains the reference to the last int [40000000]
>New int [40000] > assign reference to array to TMP
So in 1 TMP remains a reference to its last value
Try:
tmp = null; tmp = new int[40000000];
The above is all the contents of Java garbage collection in the dialog box collected and sorted by the programming home for you. I hope this article can help you solve the program development problems encountered by Java garbage collection in the dialog box.
If you think the content of the programming home website is good, you are welcome to recommend the programming home website to programmers and friends.