-2


I am working a c# - c++ (manage) mix project and we realize there are memory leaks in our project, so I searched and found out, destructor of c++ part never called so I wrote a piece of code to free memories. Now I can see program growing slower in memory (there are more memory leaks), but the problem is, in c# part program start to crash because of "out of memory exception". In operation such as

double []k = new double[65536];

Memory usage of program normally seems 400-500mb but it crashed.
OS : win server 2003
Memory : 4 GB
OS should let program to grow nearly 1200 mb but after I wrote the free memory part it start to crash 400-500mb.
I called this c++ func from the c# part to free memories

freeMemories()
{
   if(!mIsAlreadyFreedMemory)
   {
      mIsalreadyFreedMemory = true;
      _aligned_free(pointerA);
      _aligned_free(pointerB);
       ....
    }
}

Why it cannot take new memory, Program can not take the released memory again?

ozkulah
  • 66
  • 10
  • Not a duplicate but this http://stackoverflow.com/questions/1391672/what-is-the-maximum-size-that-an-array-can-hold might be relevant to your issue: consider switching from static arrays to collections – Alex Apr 18 '13 at 06:40
  • "double k = new double[65536];" this is only an example, and there are lots of different array allocations in my project, so I can not switch them, besides it is not crashing a precise point. – ozkulah Apr 18 '13 at 06:56
  • 6
    How about posting the piece of code you wrote to free the memory? Also, be aware that memory fragmentation can cause "out of memory" when there is apparently plenty of memory available because there are no sufficiently large continuous blocks left. – Matthew Strawbridge Apr 18 '13 at 07:25
  • I add freememory part but problem is not taking c++ memory allocation. (it can take align memory again). C# part can not. – ozkulah Apr 18 '13 at 07:53
  • 1
    `double k = new double[65536]` is incorrect in both C# and C++ – Marius Bancila Apr 18 '13 at 07:54
  • @Oz The effort you put into making your question correct and fixing typos is a *big* indicator to the S.O. community that you're not trying to [mooch](http://en.wiktionary.org/wiki/moocher). While it may not fix your problem, it's an important part of making S.O. a useful tool for future users. – Chris Pfohl Apr 18 '13 at 13:01
  • I closed my freememories() func and I started prog again. It fastly growed and nearly 1200mb it crashed. So the problem can be memory fregmantation, but how will I solve this? – ozkulah Apr 18 '13 at 13:02
  • @Christopher you are right, I will try to be carefull. – ozkulah Apr 18 '13 at 13:07

2 Answers2

2

You should use the IDisposable pattern for your objects. The basic idea is this:

public class MyObject : IDisposable
{
    // Some 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 you can call Dispose on your object as soon as you're sure it is no longer needed, and the unmanaged memory will be freed.

MyObject obj;
obj.Dispose();

Or, if you forget to call Dispose, the unmanaged resources will be freed when the object is collected by the garbage collector (through the finalizer).

Daniel A.A. Pelsmaeker
  • 47,471
  • 20
  • 111
  • 157
0

I found out a solution for my problem. Cause of problem is memory fregmentation. In my project there are lots of big (0.5 mb * (15-20 allocation)) aligned memory allocation in c++ part of project, so after allocating and freeing memories, continously. There is enough memory for future operations but it is splitted to small parts. To prevent this I used memory pool pattern. Instead of taking lots of allocation. I allocate one big pool and used it for all arrays with pointers. So in c# part operations such as;
double []k = new double[65536];
operations don't cause problem now.

ozkulah
  • 66
  • 10