我如何从程序中删除内存 [英] How can i remove memory from program

查看:64
本文介绍了我如何从程序中删除内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个类,两个类都很大并且占用大量内存,我为class1(HugeClass1)保留了内存,然后有了
进行计算并将结果保存到文件中.现在因为第二类(HugeClass2)也很大
我想在我为calss(HugeClass2)保留内存之前从内存中删除calss1(HugeClass1).我该怎么办?

I have 2 classes, both are huge and consume a lot of memory, i reserved memory for class1(HugeClass1),then i have
made calcuation and saved the one result to a file. Now because the 2nd class(HugeClass2) is huge too
i want to REMOVE calss1(HugeClass1) from memory before i reserve memory for calss(HugeClass2). how can i do that ?

  class HugeClass1
    {   
        public NodeS(int Nii, int Njj, int Nkk)
        {  int i, j, k;
				.
				.
				.
		}
	}
    
	class HugeClass2
    {   
        public NodeS(int Nii, int Njj, int Nkk,int Nmm)
        {  int i, j, k,m;
				.
				.
				.
		}
	}

main()
{int Ni=1000,Nj=1000,Nk=1000,Nm=1000;

HugeClass1 xf = new HugeClass1(Ni, Nj, Nk);
//do calculation
xf.Save2File();//save to file
//  ??					//<-------  i want to remove HugeClass1 from memory in this line  
//reserve again from memory
HugeClass2 xf2 = new HugeClass2(Ni, Nj, Nk,Nm);
.
.
.

}

推荐答案

实现 IDisposable [ ^ ],然后手动将其丢弃,或将其包含在using块中:
Implement IDisposable[^] on your class, and either Dispose it manually, or enclose it in a using block:
using (HugeClass1 xf = new HugeClass1(Ni, Nj, Nk))
   {
   //do calculation
   xf.Save2File();//save to file
   }

这并不意味着您的应用程序将使用更少的内存(.NET不会释放内存,直到系统开始耗尽物理内存为止)

这也可能无济于事:在大型对象堆上分配的对象(对象> = 85,000字节)不会被垃圾收集器压缩.微软认为,移动这些对象的成本太高了.最好将类更改为使用一个较小的对象,而不是使用一个更大的对象.

This doesn''t mean that your app will use less memory (.NET doesn''t let go of memory until the system starts to run out of physical memory)

It also may not help much: Objects allocated on the large object heap (objects >= 85,000 bytes) are not compacted by the garbage collector. Microsoft decided that the cost of moving those objects around would be too high. It may be better to change your class to use more, smaller objects than one huge object.


这篇关于我如何从程序中删除内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆