2

I have a memory leak in my java game application, that I was somehow expecting. The leak comes from a new instance being created multiple times on this button action listener, because each time I press the button it creates a new instance of RegularMode:

btnRegular.addActionListener(new ActionListener() {
 @Override
 public void actionPerformed(ActionEvent e) {
 frame.remove(pane);
 gm = Gamemode.REGULAR;
 mode = new RegularMode(frame, WIDTH, HEIGHT);
 }
});

Funny thing is, I have been trying to fix the memory leak, using this code:

public static void initDisplay() {
 gm = Gamemode.NONE;
 mode.setRunning(false);
 frame.remove(mode.getPane());
 frame.add(pane);
 frame.validate();
 frame.repaint();
 mode = null; // THIS LINE
 frame.pack();
}

– but it doesn't work. Are there any other ways to solve this type of memory leak?

Kara
6,23616 gold badges54 silver badges58 bronze badges
asked Nov 2, 2013 at 21:53
10
  • 1
    I'm afraid this is not enough to describe a memory leak. The actionListener alone cannot create a leak, because Java is garbage-collected. Normally, when you assign to mode, its old value should become eligible for collection. So, you should try to explain why it does not become eligible. Commented Nov 2, 2013 at 21:57
  • More generally, if the values do not become eligible for collection because they get cached by other objects, a solution may be to make the other objects use WeakReferences rather than normal ones, so they cannot prevent stale values from being garbage-collected. Commented Nov 2, 2013 at 21:58
  • 1
    no. it doesn't, because the garbage collector should collect and delete the old instances you're not using anymore. The listener code is perfectly ok, if taken alone. You should analyse all the code using the mode field, and try to understand why references are not made garbage-collectable. Commented Nov 2, 2013 at 22:02
  • 1
    Are you sure you get a memory leak? do you just see the used memory increase or do you get a OutOfMemoryException? Commented Nov 2, 2013 at 22:04
  • 3
    @TheProjectCodex: how do you know there is a memory leak? My guess is that there isn't any. Commented Nov 2, 2013 at 22:04

2 Answers 2

5

I am not sure how you came to concluding that the code you provided is causing the memory leak. Use some profiler to see what objects are currently in heap and accumulating. You can search for profilers or check this: http://jyops.blogspot.se/2012/09/java-memory-model-simplified.html

answered Nov 2, 2013 at 22:09
Sign up to request clarification or add additional context in comments.

Comments

0

You could try adding a call to System.gc() after setting mode to null.

You also could try using a different garbage collector than the default. To do so, pass in -XX:[Garbage collector] (e.g. -XX:ConcMarkSweep) when running your app. If you're in Eclipse, you have to set this in your project configuration (arguments to pass to the JVM). There is a list of the available collectors here.

You also can increase the cap on the amount of memory the jvm can use which default to something like 256mb. To do this you have to pass in -Xmx 1024M (or something like that).

Hope this helps.

answered Nov 2, 2013 at 22:10

2 Comments

Yes, this is true. It also merely requests that the JVM garbage collect, and doesn't force it to.
It's generally best to not expect System.gc() to do anything at all predictable. It's mostly there for people who feel they must somehow control GC, even when they don't have the foggiest notion what they're doing.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.