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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With