#668 – GetTotalMemory Indicates How Much Memory You’ve Allocated

You can call the GC.GetTotalMemory function to find out the total # bytes you’ve allocated so far on the managed heap.  Note that this number does not include other memory that your process may have allocated, including things like memory allocated in unmanaged code.

        static void Main()
                Console.WriteLine(string.Format("Before we start, total mem alloc'd {0} bytes", GC.GetTotalMemory(false)));

                while (true)
                    ConsoleKeyInfo cki = Console.ReadKey();

                    int size = 1024 * 1024 * 100;  // 100MB
                    byte[] data = new byte[size];

                    Array.Clear(data, 0, size);


                    Console.WriteLine(string.Format("Total mem alloc'd now {0} bytes", GC.GetTotalMemory(false)));
            catch (Exception xx)


About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

2 Responses to #668 – GetTotalMemory Indicates How Much Memory You’ve Allocated

  1. David says:

    The comment should be “// 100MB”.

    • Sean says:

      Thanks David. Corrected. Technically, Mebibyte rather than Megabyte. But no self-respecting software developer would stoop low enough to nit-pick decimal vs. binary version of MB.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: