私はJava SWTアプリケーションを持っています。コンテナのサイズ変更メソッドで、次のコードを使用して表示される画像のサイズを変更したいと思います。ただし、すべてを破棄しても(本当に?)メモリ消費量が常に増加しているようです...間違いが見つかりません。このコードがすべてのメモリを消費する理由と場所は?
サイズ変更リスナーは次のとおりです。
tabCover.addListener(SWT.Resize, new Listener() {
public void handleEvent(Event event) {
// Set images: cover_front
int width = tabCover.getSize().x - 30;
int height = tabCover.getSize().y - 30;
Image buffer_Coverfront;
buffer_Coverfront = new Image(Display.getDefault(), filename);
lblCoverfront.setImage(Helper.ImageScale(buffer_Coverfront, width, height));
buffer_Coverfront.dispose();
buffer_Coverfront = null;
} // handleEvent
}); // Listener
そして、ここでスケーリング関数:
public static Image ImageScale(Image image, int width, int height) {
ImageData data = image.getImageData();
// Some logic to keep the aspect ratio
float img_height = data.height;
float img_width = data.width;
float container_height = height;
float container_width = width;
float dest_height_f = container_height;
float factor = img_height / dest_height_f;
int dest_width = (int) Math.floor(img_width / factor );
int dest_height = (int) dest_height_f;
if(dest_width > container_width) {
dest_width = (int) container_width;
factor = img_width / dest_width;
dest_height = (int) Math.floor(img_height / factor);
}
// Image resize
data = data.scaledTo(dest_width, dest_height);
Image scaled = new Image(Display.getDefault(), data);
image.dispose();
return scaled;
}