Home > DeveloperSection > Blogs > Garbage Collector Class in C#

Garbage Collector Class in C#

C# C# 
0 Comment(s)
 1996  View(s)
Rate this:

Garbage Collector Class in C#

In this blog I am trying to explain the concept of garbage collector class in C#.

Garbage class

In this class impact when garbage collection is performed on an object and when resources allocated by an object are released. Properties in this class provide information about the total amount of memory available in the system and the age category, or generation, of memory allocated to an object.

The garbage collector tracks and reclaims objects allocated in managed memory. Periodically, the garbage collector performs garbage collection to reclaim memory allocated to objects for which there are no valid references. Garbage collection happens automatically when a request for memory cannot be satisfied using available free memory. Alternatively, an application can force garbage collection using the Collect method.

·         Garbage collection consists of the following steps:

·         The garbage collector searches for managed objects that are referenced in managed code.

·         The garbage collector tries to finalize objects that are not referenced.

·         The garbage collector frees objects that are not referenced and reclaims their memory.

Example: -

1.  using System;

2.  namespace GCCollectInt

3.  {

4.  class MyGCCollectClass

5.  {

6.  private const long maxGarbage = 1000;

7.  static void Main()

8.  {

9.  MyGCCollectClass myGCCol = new MyGCCollectClass();

10. Console.WriteLine("The highest generation is {0}", GC.MaxGeneration);

11. myGCCol.MakeSomeGarbage();

12. Console.WriteLine("Generation: {0}", GC.GetGeneration(myGCCol));

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

14. GC.Collect(0);

15. Console.WriteLine("Generation: {0}", GC.GetGeneration(myGCCol));

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

17. GC.Collect(2);

18. Console.WriteLine("Generation: {0}", GC.GetGeneration(myGCCol));

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

20. Console.Read();

21. Console.ReadKey();

22. }

23. void MakeSomeGarbage()

24. {

25. Version vt;

26. for (int i = 0; i < maxGarbage; i++)

27. {

28. vt = new Version();

29. }

30. }

31. }

32. }

Code description: -

1.       Using System (include the System namespace).

2.  Create namespace with GCCollectInt name.

4.  Create class with MyGCCollectClass name.

6.  Create variable private const long with maxGarbage name and its initial value 1000

7.       Create Main method.

8.  Create MyGCCollectClass object with myGCCol name and initialize with new MyGCCollectClass();

10.   Determine the maximum number of generations the system garbage collector currently supports.

11.   Determine which generation myGCCol object is stored in.

13.   Determine the best available approximation of the number of bytes currently allocated in managed memory.

14.   Perform a collection of generation 0 only.

15.   Determine which generation myGCCol object is stored in.

17.   Perform a collection of all generations up to and including 2.

18.   Determine which generation myGCCol object is stored in.

22. Create method with MakeSomeGarbage()named.

23.   Here create a for loop which is run 1000 time because maxGarbage value is 1000.

28.   Create objects and release them to fill up memory with unused objects.

Output: -

Don't want to miss updates? Please click the below button!

Follow MindStick