0

My Java (.jar) program accesses the database and stores values as private ArrayList <String []> (Global variables) to be used multiple times in the JFrame that's displayed.

The data is stored as follows: Each row of a given table is stored as an Array inside the ArrayList. Thus this ArrayList contains all the needed data from the given table. Using this method increases the speed of my program because accessing data multiple times using the pre-stored ArrayList is faster than accessing the database itself.

The problem is that whenever JFrame.dispose() is executed in order to open another JFrame, the ArrayList's allocated space is not liberated by the Garbage Collector (even though there are no references to those ArrayLists anymore) and keeps taking up (and stacking) space on the Heap Memory. Every time I dispose, then enter another frame multiple ArrayLists of a few KBs to a few MBs are created depending on what data the JFrame needs. In the end the maximum Heap Space is reached and a java.lang.OutOfMemoryError: Java heap space error is thrown.

Unless the program is closed and opened again, the memory keeps stacking up. Note that System.gc() does not work.

A JFrame example:

import java.util.ArrayList;

public class Test extends javax.swing.JFrame {

   private javax.swing.JButton GenerateB;

    public Test() {
        initComponents();
    }

    //To create the JButton:
    private void initComponents() {
        GenerateB = new javax.swing.JButton();
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
        GenerateB.setText("Generate");
        GenerateB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                GenerateBActionPerformed(evt);
            }
        });
        getContentPane().add(GenerateB, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, -1, -1));
    }

    //Code executed when the button is clicked. 
    //Clicking the button tens of times will fill the memory heap till it runs out of memory, even though there is no reference to the ArrayLists created
    //Note: calling Text.dispose() will not free the used up space.
    private void GenerateBActionPerformed(java.awt.event.ActionEvent evt) { 
        ArrayList<String[]> A = new ArrayList<>();
        for (int i = 0; i < 100000; i++) {
            String[] S = {i + "", i + i + "", i + i + i + "", i + i + i + i + ""};
            A.add(S);
        }
    }
}

Is there a way to "clean" the heap memory of those Arrays/ArrayLists? Am I doing something wrong?

Elio
  • 148
  • 9
  • What do you mean by global variable? Assuming it's static, is it explicitly set to null so it will become eligible for GC? Post some code. – Andrew S Jul 28 '17 at 16:49
  • @AndrewS I've place an example. – Elio Jul 28 '17 at 17:13
  • Unable to reproduce - called `GenerateB.doClick()` 1,000 times with the same instance and with a new instance on each iteration. – Andrew S Jul 28 '17 at 18:38

0 Answers0