Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unmanaged memory not showing up in task manager

Tags:

c#

unmanaged

I wrote the following test (actually used in a wider context)

IntPtr x = Marshal.AllocHGlobal(100000000);

Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);

Marshal.FreeHGlobal(x);

Console.ReadKey(true);

Why doesn't the task manager show any sign of the allocated 100 megabytes before the first key press? If this is by design, how else can I test the consumption of unmanaged heap memory?

like image 822
oliver Avatar asked Mar 17 '18 07:03

oliver


1 Answers

You are getting some insight in how your operating system works. This behavior is not specific to Marshal.AllocHGlobal(), try this code for example:

    static void Main(string[] args) {
        var arr = new byte[100000000];
        Console.ReadKey(true);
    }

Task Manager by default shows you how much RAM you use. But your OS, like many others, is a demand-paged virtual memory operating system. You haven't demanded anything yet. All you did is allocate virtual memory. Just address space, it stays virtual in this test, just numbers to the processor. One for each 4096 bytes. The demand doesn't happen until you actually access the array. Add:

        for (int ix = 0; ix < arr.Length; ix += 4096) {
            byte dummy = arr[ix];
        }

Bam, now you see it zoom up. Not necessarily to 100 megabytes, but most machines now have enough RAM to not need any pages to be swapped back out to the paging file to provide enough storage.

Otherwise a good reminder why Task Manager is not a very good memory profiler. How much RAM your program consumes is pretty irrelevant. If you use too much then you'll notice it well enough, your program slows down a lot.

Task Manager can show you the side-effect of the original code, you have to add the "Commit size" column. Named differently on earlier Windows versions, fuzzy memory, I think it was VM Size. Commit size measures how much space is reserved in the paging file. Backup storage for the address space, reserved so the RAM content can be stored when the OS needs to unmap pages to provide RAM elsewhere. Windows does not permit over-committing memory, it is the way you get OOM in a 64-bit program when it can't grow the paging file quickly enough.

like image 83
Hans Passant Avatar answered Sep 20 '22 15:09

Hans Passant