我的表单上有一些按钮。当我点击每个按钮时,使用相同的按钮运行新表单。当我多次点击时,显示错误OutOfMemory 我想这是因为我创建了很多表单对象 如果有形式,可以从堆栈中清除堆栈或使用表单吗?
答案 0 :(得分:3)
您正在保留指向旧组件的指针(引用),这会导致内存泄漏。确保永远不要将组件存储为类的成员,除非您稍后将其清除。
答案 1 :(得分:1)
您需要为代码使用Singleton模式。在Singleton Pattern中,它只会创建Form类的一个对象。如果对象为null,那么它将创建一个新的对象,它将返回当前的对象。有关此请参阅以下代码。
// Private Constructor
private static myForm thisForm = null;
private myForm()
{
thisForm = this;
}
// Now to Create Object, you need to create following getInstance Method
public static myForm getInstance()
{
if ( thisForm == null )
{
thisForm = new myForm();
}
return thisForm;
}
在整个代码中尝试上面的逻辑。你的OutOfMemory问题将100%得到解决。