简体   繁体   中英

Why is JTable scrolling taking so much memory?

Hello i am programming twin panel file manager in java and i am facing a problem.

Whenever I scroll JTable it takes about 8M more memory... Is there a way how to fix that ? It stops everytime after consuming 40 - 60M of memory. Is it a Swing component problem ?

Thank you for your responses.

Edit

I tried to understand why it takes so much memory. Now i know the source of the problem. I made a small button with this actionPerformed: jScrollPane1.repaint(); .

When I hit it 10 times i got big memory consumptions in task manager and also in VisualVM. But in VisualVM GC starts to collect on 50 MB and lowers it to 8 Mb. But windows taskmanager is still increasing its value.

The repaint method is making big memory leaks in windows. Is there any fix ?

Edit2

A further research of this problem gave me this. I tried to run this program on Linux platform with no leaking. The program had about 20 M of memory used. So i've programmed a little thread which was invoking the method repaint on both JScrollPanes . And to my suprise on Windows machine memory was rising until 110 M but then the OS started to push harder on memory.

The Thread:

@Override
public void run() {
        while (true) {
            jScrollPane1.repaint();
            jScrollPane2.repaint();
            try {
                this.sleep(10);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    }

I was doing normal copy/rename/delete operations also was going through directories with no memory rising. I noticed that the memory was also decreasing to 99M.

On the monitoring thread:

@Override
public void run() {
    while (true) {
        aLabel.setText("Memory consumption: " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory());
        try {
            this.sleep(200);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }
}

the numbers were from 8M to 50M and then again 8M. So garbage collecting was successful. So the real problem is windows platform or the compatibilty of the JVM ? As trashgod suggested that task manager is not precise in getting the memory consumptions but the memory is being really used by the java process.

Invoking FileSystemView.getFileSystemView() repeatedly may be a problem, as suggested here and profiled here . It may help to edit your question to include an sscce that demonstrates the problem. Using a profiler may suggest the scope and severity of the problem; Java VisualVM may already be installed.

Windows task manager is still increasing its value.

The Windows task manager may not be entirely dispositive, as suggested here . In this context, the jvisualvm result may be more reliable.

never use Thread#sleep during EDT, that reason why you get un_expecting UsedMemory ,

1) Swing GUI is single threaded

2) Swing GUI waits for all events done, all changes are done on screen in one moment, including usage of Thread#sleep ,

3) by using Thread#sleep you can simulating and see on the screen un_expected repaint of GUI, or value aren't refreshed or repainted

4) you have issues with Concurency in Swing

5) I can't see any reason for using repaint() there, notice that very hard method for local enviroment

6) use and wrap you code to the

7) since I have a few methods when is required (deliberate) usage Thread#sleep , not easy job without Controler covered whole EDT

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM