This is a discussion on Garbage Collection C# within the C# Programming forums, part of the Software Development category; Hi, Now, I will explain about the concepts of Garbage Collection. Garbage Collection basis : * Almost every program uses resources such ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| Hi, Now, I will explain about the concepts of Garbage Collection. Garbage Collection basis : * Almost every program uses resources such as database connection, file system objects etc. In order to make use of these things some resources should be available to us. * First we allocate a block of memory in the managed memory by using the new keyword. (This will emit the newobj instruction in the Microsoft intermediate language code generated from C#, VB.NET, Jscript.NET or any other .NET compliance language). * Use the constructor of the class to set the initial state of the object. * Use the resources by accessing the type’s members. * At last CLEAR THE MEMORY.
__________________ S.Balasubramanian Nothing is impossible |
| Sponsored Links |
| |||
| System.GC : System.GC class is used to controls the system garbage collector, a service that automatically reclaims unused memory. When an object is garbage collected and when resources allocated by an object are released they are influence through the methods of this class. The garbage collector tracks and reclaims objects allocated in managed memory. Garbage collection consists of the following steps: 1. The garbage collector searches for managed objects that are referenced in managed code. 2. The garbage collector attempts to finalize objects that are not referenced. 3. The garbage collector frees objects that are not referenced and reclaims their memory.
__________________ S.Balasubramanian Nothing is impossible |
| |||
| Ex : public partial class GarbageCollectionClass : Form { private const long maxGarbage = 1000; public GarbageCollectionClass() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { GarbageCollectionClass garbageCollectionClass = new GarbageCollectionClass(); // Determine the maximum number of generations the system // garbage collector currently supports. MessageBox.Show("The highest generation is " + GC.MaxGeneration); garbageCollectionClass.CreateSomeGarbage(); // Determine which generation garbageCollectionClass object is stored in. MessageBox.Show("Generation: " + GC.GetGeneration(garbageCollectionClass)); // Determine the best available approximation of the number // of bytes currently allocated in managed memory. MessageBox.Show("Total Memory: " + GC.GetTotalMemory(false)); // Perform a collection of generation 0 only. GC.Collect(0); // Determine which generation garbageCollectionClass object is stored in. MessageBox.Show("Generation: " + GC.GetGeneration(garbageCollectionClass)); MessageBox.Show("Total Memory: " + GC.GetTotalMemory(false)); // Perform a collection of generation 2 only. GC.Collect(2); // Determine which generation myGCCol object is stored in. MessageBox.Show("Generation: " + GC.GetGeneration(garbageCollectionClass)); MessageBox.Show("Total Memory: " + GC.GetTotalMemory(false)); } private void CreateSomeGarbage() { Version version; for (int i = 0; i < maxGarbage; i++) { // Create objects and release them to fill up memory // with unused objects. version = new Version(); } } }
__________________ S.Balasubramanian Nothing is impossible |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How can I force garbage collection to take place in java? | mobilegeek | Java Programming | 2 | 08-22-2007 08:25 AM |
| garbage collection | anbuchezhians | Java Programming | 3 | 08-21-2007 02:46 AM |
| Using Garbage Collection in Java ME | itbarota | J2ME | 2 | 08-06-2007 09:00 AM |
| garbage collector | sivakumar | Java Programming | 2 | 07-20-2007 07:39 AM |
| Garbage Collector | nhoj | ASP and ASP.NET Programming | 1 | 07-17-2007 02:32 AM |