简体   繁体   中英

Trying to find memory leak in my java swing code

I have a sizable Java app that creates complex data structures describing a drawing, starting with the raw time-stamped data points and displaying the drawing, analyzing it, etc.

When I try to process a series of these drawings, I'm clearly hanging on to memory in the form of a JScrollPane that's used to hold the drawing, but I cannot figure out why. The variable holding the scroll pane is reassigned with each new file loaded, but I noticed that the swing RepaintManager is maintaining a list of the previously displayed panes in its invalidComponents list. This seems then to hang onto the storage for each drawing, and before long I've got 1GB of memory in use when I'm processing files serially and should thus never have more than one drawing's worth of memory in use.

I got a memory dump and have analyzed with the Eclipse memory analyzer and with YourKit.

Here's what I believe to be the smoking gun as displayed by Eclipse's memory analyzer:

在此处输入图片说明

Have spent quite a while trying to drill down into this problem. I would be most grateful for any suggestions about where my error may lie or how to work around the problem.

I realize it's difficult to tell without being able to dig into the code, but if there are even any general suggestions/cautions about where to look, that would be great.

As @MadProgrammer suggests , your present code presumably replaces the existing JScrollPane instance:

scrollPane = new JScrollPane(view);

Instead, update the viewport component via setViewportView() :

scrollPane. setViewportView(view);

Even better, update the content of view directly. Details would depend on your implementation, but validate() and repaint() represent one approach.

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