C# cannot take new memory -
working c# - c++ (manage) mix project , realize there memory leaks in our project, searched , found out, destructor of c++ part never called wrote piece of code free memories. can see program growing slower in memory (there more memory leaks), problem is, in c# part program start crash because of "out of memory exception
". in operation such as
double []k = new double[65536];
memory usage of program seems 400-500mb crashed.
os : win server 2003
memory : 4 gb
os should let program grow 1200 mb after wrote free memory part start crash 400-500mb.
called c++ func c# part free memories
freememories() { if(!misalreadyfreedmemory) { misalreadyfreedmemory = true; _aligned_free(pointera); _aligned_free(pointerb); .... } }
why cannot take new memory, program can not take released memory again?
you should use idisposable
pattern objects. basic idea this:
public class myobject : idisposable { // unmanaged resource: unmanagedresource unmanaged; // finalizer: ~myobject { disposeunmanaged(); } public void dispose() { disposemanaged(); disposeunmanaged(); gc.suppressfinalize(this); } protected virtual disposemanaged() { // dispose _managed_ resources here. } protected virtual disposeunmanaged() { // dispose _unmanaged_ resources here. this.unmanaged.freeme(); this.unmanaged = null; } }
then can call dispose
on object you're sure no longer needed, , unmanaged memory freed.
myobject obj; obj.dispose();
or, if forget call dispose
, unmanaged resources freed when object collected garbage collector (through finalizer).
Comments
Post a Comment