Resizing processing graphics surface embedded in JFrame without memory leaks

I have the following code and am running this on both Windows 7 and Mac OSX 10.9, and when I resize I can see the memory increase as I am resizing. Is there a way to allow resizing without memory leaks?

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import processing.core.PApplet;

public class TestJMenuWin {

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setBounds(10, 10, 500, 500);
                final PApplet p = new PApplet() {
                    private static final long serialVersionUID = 1L;

                    @Override
                    public void setup() {

                    }

                    @Override
                    public void draw() {
                        background(0);
                        rect(width / 3, height / 3, width / 3, height / 3);
                    }

                };

                p.frame = frame;
                p.setupFrameResizeListener();
                frame.add(p);
                frame.setVisible(true);
                p.init();
            }
        });
    }
}

Answers

  • Maybe this was the wrong section to post this?

  • What makes you think it's a memory leak?

  • Well, the memory just keeps increasing when I look at the application's memory usage in the Mac activity monitor, or, on windows, on the task manager every time i resize the window. Would you happen to know if I am resizing the processing graphics surface correctly?

  • Seems fine to me. But just because your memory is going up doesn't mean you have a memory leak. It could be the case that the garbage collector simply hasn't kicked in yet. Do you encounter any actual errors, or are you just assuming there's a memory leak because your memory goes up a little?

  • Testing it with a profiler on Mac has shown me that it looks like it keeps increasing because the JVM heap size increases to a number far above than what is actually needed by the application probably because the garbage collector can't work fast enough to remove the old processing graphics.

    It looks like I'm having more issues on Windows though where I can see the memory increase much more significantly to gigabytes, but I haven't been able to get my profiler working on Windows yet.

  • Gigabytes of memory? Are you sure you're looking at the right thing? Maybe you're looking at a total of something, and not a current snapshot? Can you post a screenshot of what you're looking at?

    You'd have to modify your Java settings to even be able to get to gigs of memory. And do you see any errors being thrown?

  • Okay, I found the issue I was having was with the renderer I was using. I was actually finding problems with a different application I'm working on and thought I was seeing the same memory issues with this mini program I created. It turns out that the issues I was seeing with my other application was caused by using the P2D renderer. Once I took that out and used the default renderer like I am doing in this mini application, then things were working very well.

    Sorry, for that and thanks for your time!

Sign In or Register to comment.