[英]java swt memory leak
我有java swt代码,其中存在内存泄漏,我可以找到它在哪里,但无法解决它。 这是我的代码,有内存泄漏,
void refreshMyPanel(){
Control[] oldControls = my_group.getChildren();
//do sth
for(int i = 0; i < 6; i++){
Label label1_lin = new Label(linear_img_group, SWT.NONE);
// first option
// label1_lin.setImage(SWTResourceManager.getImage( "C:\\Users\\Public\\Pictures\\Sample Pictures\\p1.jpg"));
// second option
// label1_lin.setImage(SWTResourceManager.getImage(linear_list.get(i)));
label1_lin.setBounds(x0, y0, xSize, ySize);
}
for (Control oldControl : oldControls)
oldControl.dispose();
//do sth
}
我调用了60次refreshMyPanel()
方法,当我在60结束时取消对first option
注释时,Java SWT应用程序的内存使用量约为60 MB,而当我调用second option
则变为360 MB。
最有可能的是,它不会自动处理图像,但是我将如何处理呢?
我在Windows环境中使用eclipse kepler。
其他测试 :当我得到图像时
Image img = new Image(display, linear_list.get(i);
labe1_lin.setImage(img);
内存使用量变为1.3 GB左右,而当我处理它时,它再次给出异常。 谢谢,
您应该执行以下操作:
void refreshMyPanel(){
Control[] oldControls = my_group.getChildren();
for (Control oldControl : oldControls)
oldControl.dispose();
for(int i = 0; i < 6; i++){
Label label = new Label(linear_img_group, SWT.NONE);
Image image = new Image(Display.getDefault(), linear_list.get(i));
label.setImage(image);
label.addListener(SWT.Dispose, new Listener()
{
@Override
public void handleEvent(Event e)
{
Label thisLabel = (Label) e.widget;
Image thisImage = thisLabel.getImage();
if(!thisImage.isDisposed();
thisImage.dispose();
}
});
label1_lin.setBounds(x0, y0, xSize, ySize);
}
}
另外,创建6 Image
一度你运行你的100迭代循环之前,使用它们没有处置refreshMyPanel
,然后dispose
他们,一旦你做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.