donnyton
donnyton

Reputation: 6054

ImageIcon loading getting slower and slower

In my java GUI application, I have some components 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).

Upvotes: 1

Views: 959

Answers (2)

Stephen C
Stephen C

Reputation: 718708

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.

Upvotes: 0

Andrew Thompson
Andrew Thompson

Reputation: 168815

Have a look at The Perils of Image.getScaledInstance(). It might not be the problem here, but you won't know until trying alternatives.

Upvotes: 2

Related Questions