Chưa phân loại

Garbage Collector in C# – When we shouldn’t rely on it

Most people know that Gargabe Collector (GC) in C# will manage all objects we create in our program. It means, the GC know when a object is non-reference and determine when it is destroyed.
Let’s see the simple program bellow see how to the GC work?

	using System;
	using System.Collections.Generic;

	namespace GcTest
	{
		class Program
		{
			static void Main()
			{
				const int count = 10000000;
				// Add all element to list
				var myContainter = new List<string>();
				for (int i = 0; i < count; i++)
				{
					myContainter.Add(i.ToString());
				}

				Console.WriteLine("After adding all elements to list");

				Console.WriteLine("Total memory: {0}", GC.GetTotalMemory(false));

				Console.ReadKey();

			}
		}
	}
	

The output is:
After adding all elements to list
Total memory: 346791356

In this code, all we know before the program exit, GC will automatically release all memory and then go to exit the program.

Now, what’s happened when we explicitly make all objects in myList null. Whether the GC will release all memory of them? Take a simple code to check that:

	using System;
	using System.Collections.Generic;

	namespace GcTest
	{
		class Program
		{
			static void Main()
			{
				const int count = 10000000;
				// Add all element to list
				var myContainter = new List<string>();
				for (int i = 0; i < count; i++)
				{
					myContainter.Add(i.ToString());
				}

				Console.WriteLine("After adding all elements to list");

				Console.WriteLine("Total memory: {0}", GC.GetTotalMemory(false));

				// Release all elements in list by setting all with null
				for (int i = 0; i < count; i++)
				{
					myContainter[i] = null;
				}

				myContainter = null;

				Console.WriteLine("After releasing memory");

				Console.WriteLine("Total memory: {0}", GC.GetTotalMemory(false));

				Console.ReadKey();

			}
		}
	}
	

The output is:
After adding all elements to list
Total memory: 346794912
After releasing memory
Total memory: 346794912

It’s unbelievable. The GC did nothing dispite myList is null?

In some cases, we may want to decrease memory of our program for some purposes.
In C/C++, we can easily do this by using free/delete and in C# we alse do this by using

	GC.Collect
	

When we call Collect function that means we want GC immediately destroy unuse objects.

	using System;
	using System.Collections.Generic;

	namespace GcTest
	{
		class Program
		{
			static void Main()
			{
				const int count = 10000000;
				// Add all element to list
				var myContainter = new List<string>();
				for (int i = 0; i < count; i++)
				{
					myContainter.Add(i.ToString());
				}

				Console.WriteLine("After adding all elements to list");

				Console.WriteLine("Total memory: {0}", GC.GetTotalMemory(false));

				// Release all elements in list by setting all with null
				for (int i = 0; i < count; i++)
				{
					myContainter[i] = null;
				}

				myContainter = null;

				Console.WriteLine("After releasing memory");

				Console.WriteLine("Total memory: {0}", GC.GetTotalMemory(false));

				// Call Collect function to release unuse memory
				GC.Collect();

				Console.WriteLine("After calling Collect function");

				Console.WriteLine("Total memory: {0}", GC.GetTotalMemory(false));

				Console.ReadKey();

			}
		}
	}
	

The output is:
After adding all elements to list
Total memory: 346795224
After releasing memory
Total memory: 346795224
After calling Collect function
Total memory: 92936

Now, unuse memory is actually destroyed. And now, I’m sure that some people intend to use this function whenever they want to clean memory, but be carefull because it’ll take a big overhead in your program. Your idea may be usefull in case when you want actually release the memory and I know almost cases, we have not to do this, let the GC does it responsibility normally.

Hope this post help you in some case.

Advertisements

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 )

Google+ photo

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

Connecting to %s