Find the memory allocation using GC.

We can find the memory allocation using the following code.
Following is the code for demonstrating the memory allocation

using System;
public class MyClass {

public void Dispose() {
Console.WriteLine("Dispose() method is called");

public class MyClientClass {

public static void MyAction() {
MyClass Test = new MyClass();


public static void Main(string[] args) {
long Mymemory = GC.GetTotalMemory(true);
Console.WriteLine("At the begin {0} bytes allocated", Mymemory);
Mymemory = GC.GetTotalMemory(true);
Console.WriteLine("At the end {0} bytes allocated", Mymemory);

Code Explanation

1. Create MyClass
2. For differentiate the memory allocation call the dispose method there
3. Create the MyClientClass for get the memory allocation
4. Call the GC.GetTotalMemory for getting the memory allocation
5. Call MyAtion() method
6. Call the GC.GetTotalMemory again for getting the memory allocation
7. We can see the differences



No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: