I am having a memory leak in my java version of the game Snake. I have narrowed the exact step of when the program memory is increased to two possible points. The last step before the my game timer runs, (this.pack()), or the first execution of the game/pregame menu timer's loops run. The actually object that's eating up my memory is an array of ints which grows and grows every time the game is restarted or resized. (Includes canceling of timers and creating of new ones and new timer tasks.
Whenever this.pack() is called from my View class(Extends JFrame, which in turn extends the class window where pack is found). This is a problem because my game includes methods which change the background and resize the game which need to call the pack method or the game window will not work right. After many hours of trying to fix this I am still unable, and I am not really sure why this.pack() is increasing the memory of the program.
I chased down the first execution of my game/pregame timer's loop and the only possible source of the problem would be the drawing of the background, which looks like this:
Dimension currentSize = getSize();
g.drawImage(startingBackround, 0, 0, currentSize.width , currentSize.height, null);
startingBackground (spelled wrong in my code) is a BufferedImage created in the constructor.