IT Community - Software Programming, Web Development and Technical Support

Garbage Collection C#

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 ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C# Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 03-17-2008, 09:18 PM
Balasubramanian.S Balasubramanian.S is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 182
Balasubramanian.S is on a distinguished road
Default Garbage Collection C#

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-17-2008, 09:33 PM
Balasubramanian.S Balasubramanian.S is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 182
Balasubramanian.S is on a distinguished road
Default Re: Garbage Collection C#

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-17-2008, 09:44 PM
Balasubramanian.S Balasubramanian.S is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 182
Balasubramanian.S is on a distinguished road
Default Re: Garbage Collection C#

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT -7. The time now is 04:48 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0