1

I've created a Minesweeper game that generates a 2-D array of JButtons every time a new game is started. The problem is that memory usage increases exponentially(JProfiler says it's the JButtons). It seems that not only are the old Jbutton instances being kept in memory, but the number of instances double. How can I tell it to get rid of the old JButtons? Thanks

private JButton[][] but;
but = new JButton[row][col];
for (int i = 0;i<row;i++)
{    
  for (int j = 0;j<col;j++){
      but[i][j]= new JButton();
      but[i][j].setName(i+":"+j);
      mine.add(but[i][j]);
      but[i][j].addMouseListener(this);
  }
 }
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Why don't the old instances go away? – Thorbjørn Ravn Andersen Sep 11 '12 at 20:23
  • Do you ever delete the buttons from the component when adding the new ones? Apart from that, it does not look like a lot like a memory leak. PS: Tyler answer of reusing instead of recreating is a good one. – SJuan76 Sep 11 '12 at 20:24
  • Also, do you mean exponentially literally or it is just a way of meaning "very quickly"? – SJuan76 Sep 11 '12 at 20:25
  • Probably the mine.add(but[i][j]); is maintaining the reference to the old instances. – dan Sep 11 '12 at 20:26
  • Exponentially, they double each time 100-200-400-800...etc – William Grunow Sep 11 '12 at 20:26
  • Before this code executes I have a mine =new JPanel(); – William Grunow Sep 11 '12 at 20:27
  • Try explicitly destroying the old one? Are you just hiding it? – Tyler Eaves Sep 11 '12 at 20:28
  • 1
    I think this is "geometrically". Anyway, usually it should grow aritmetically (100-200-300). Are you sure your code is not inside a loop? I would advise logging an statement each time you create a new Button to check why it rises so sharply. – SJuan76 Sep 11 '12 at 20:31
  • How would one explicity destroy the old one? – William Grunow Sep 11 '12 at 20:33
  • If you are creating a new mine panel each time, then maybe something is holding the reference to the old one. Anyway you need to check with the profile who is holding the references, by examining the denominator tree. Probably JProfiler has this feature too, if not I suggest you to try the Eclipse MAT. – dan Sep 11 '12 at 20:34
  • I tried mine.removeAll() which didnt help :( – William Grunow Sep 11 '12 at 20:46
  • ....figured it out, seems as if I was creating a new "New Button" button each time and the function would be called several times each time – William Grunow Sep 12 '12 at 11:20

1 Answers1

4

This sounds like a classic use case for the pool pattern.

Allocate ONE grid's worth of buttons on program start, and reuse them for each game.

Tyler Eaves
  • 12,879
  • 1
  • 32
  • 39
  • But each new game might have a different size grid – William Grunow Sep 11 '12 at 20:25
  • Ok, so use a 1d grid, size i*j when i and j represent the maximal number of buttons you might need. Get a given cells address with `(row * numrows)+col`. This way the existing structure will work with any number of cells up to whatever max you allocate. – Tyler Eaves Sep 11 '12 at 20:27