.Net Hosting API Memory usage

277 views Asked by At

I'm trying to gather memory information that used by AppDomain by using .Net Hosting API. I've implemented custom IHostMemoryManager and IHostMalloc classes on C++, and specified them during creation of CRL Runtime. It similar like in that article - http://www.codeproject.com/Articles/418259/CLR-Hosting-Customizing-the-CLR-Part-2

My main idea was that IHostMAlloc::Alloc will be called for each allocation. I added counter that just counts cbSize parameter, and display it after Runtime stops.

But the problem is that that count is different. For typical run of empty console application with Hello World it displays ~ 400kb, that seems ok. But when I use such code:

    private static StringBuilder builder = new StringBuilder();

    public static int MemoryPressure()
    {
        int count = 1024 * 1024 * 50;
        for (int i = 0; i < count; i++)
        {
            builder.Append(1);
        }

        return 0;
    }

But for that case it's still displaying ~400kb. Process during execution displays ~100mb of WorkingSet memory in Task Manager.

Is it possible to count used memory in hosting API?

P.S. I know about AppDomain.MonitoringTotalAllocatedMemorySize, but it displays total used memory by the whole AppDomain, and i want to get data for specific used memory thread.

UPDATE1: Added Alloc code that i use:

static std::map<DWORD, SIZE_T> g_allocatedMemMap;
extern int g_noAllocs;
extern int g_noFrees;
extern long g_totalAllocs;

HRESULT STDMETHODCALLTYPE MyHostMalloc::Alloc(SIZE_T  cbSize,
   EMemoryCriticalLevel eCriticalLevel,
   void** ppMem)
{
   void* memory = new char[cbSize];
   *ppMem = memory;
   // statistics
   DWORD id = ::GetCurrentThreadId();
   g_allocatedMemMap[id] += cbSize;

   g_totalAllocs += cbSize;

   g_noAllocs++;
   return S_OK;
}

g_allocatedMemMap - counts memory allocation per thread

g_totalAllocs - counts total memory allocation

g_noAllocs - number of allocations

MyHostAlloc inherits IHostMalloc interface

In my case for both empty consol app and MemoryPressure call Number of allocations is ~15000

0

There are 0 answers