In my java GUI application, I have some componen开发者_运维问答ts that need to scale repeatedly, and I am doing this with the following code:
Image newImage = myimage.getScaledInstance(width, height, Image.SCALE_REPLICATE);
ImageIcon newIcon = new ImageIcon(newImage);
this.setIcon(newIcon);
this.setSize(width,height);
//increment width, height
At first this works fine, but after many iterations (a couple hundred) the new ImageIcon() constructor is taking an inordinate amount of time (about 1 second), based on some simple time checks. The rest of the code is speedy.
Why does this happen after many iterations, and how can I make it stop slowing down as iterations increases? (Loading the images in a background thread won't work, because loading the images is not a constant-time operation).
Have a look at The Perils of Image.getScaledInstance(). It might not be the problem here, but you won't know until trying alternatives.
I recall previous Q/A's where image loading was leading to memory issues. I think you need to pursue the theory that this is a memory leak:
Confirm that this is the problem by running your JVM with GC logging enabled, and see if there's some correlation between the log messages and the observed slowdown. (It should take a minute or so to confirm, one way or another.)
Use your favourite memory profiler to try to track down the leak.
精彩评论