Click to See Complete Forum and Search --> : Garbage collection


yemiu
November 23rd, 2004, 03:03 AM
.net automated garbage collection is not good because the database connection may be tied up with the object as long as the object is not garbage collected. I have 2 questions:
+ Why dont we close the connection by ourseves? Connection.Close();
+ Why Microsoft develops automated garbage collection instead of manual freeing up unused memory?

Mr. Tomaszek
November 23rd, 2004, 03:51 AM
+ Why Microsoft develops automated garbage collection instead of manual freeing up unused memory?

Developer don't have to remenber about freeing memory. This reduces amount of memory leaks - errors - which usually occurs when one forgot to free alocated memory. To put it another way - garbage collection is for ease of use. Developer can focus on his goal, not on other unimportant stuff (that can be automated).

You can, manually run garbage collector - if you want to.

Krzemo
November 23rd, 2004, 03:56 AM
U can allways do "Connection.Close();" and it's good programming practice.

U can change GC behavior by "using" clause (and IDisposable).
U can also manually GC collect (but it is not adviced).

GC is good in cases of unexpected situations (or poor code :) ).
And somtimes GC speeds up response time (application no longer wait while freeing up memory)

Best regards,
Krzemo.

Andy Tacker
November 23rd, 2004, 04:33 AM
closing connection completely depends upon the technologu you are using...

you can simply try to put something like...

if(MySQLConn.State = ConnectionState.Open)
MySQLConn.Close()...

now, if you use inbuilt dataadpaters, you will never reach the call to "close", because adapter closes the connection when it recieved the data...

but if you are opting for a DAL, you will have to explicitly close the connection...


As Krezmo already mentioned, using IDisposable helps you avoid Garbage Collection techniques...
and its no harm to add
GC.Collect() :) in your code...

darwen
November 23rd, 2004, 04:47 AM
I've got a new article coming out (hopefully in the next couple of days) which details IDisposable and 'using'.

Have a look when it goes live.

Darwen.

yemiu
November 23rd, 2004, 03:48 PM
Thanks alot for your replies.
To darwen: Thank you, I am looking forward to your article.
Pat

kasracer
November 23rd, 2004, 07:49 PM
I've got a new article coming out (hopefully in the next couple of days) which details IDisposable and 'using'.

Have a look when it goes live.
There is a good article about this on the MSDN. I've been using the using statement in C# for everything that has a base of IDisposable.

I look forward to reading your article.

yemiu
November 23rd, 2004, 08:14 PM
Is it true that we have 2 options in order to actively (not passively) garbage collect:
1. implements IDisposal ?
2. "using" keyword.

Can someone please give me a simple example of those 2 approaches?
Thanks
Pat

Krzemo
November 24th, 2004, 12:31 AM
2 approaches are
1) IDisposable interface and using statement
2) GC.Collect()

1) USING
You create an instance in a using statement to ensure that Dispose is called on the object when the using statement is exited. A using statement can be exited either when the end of the using statement is reached or if, for example, an exception is thrown and control leaves the statement block before the end of the statement.
The object you instantiate must implement the System.IDisposable interface.
Example


// cs_using_statement.cs
// compile with /reference:System.Drawing.dll
using System.Drawing;
class a
{
public static void Main()
{
using (Font MyFont = new Font("Arial", 10.0f), MyFont2 = new Font("Arial", 10.0f))
{
// use MyFont and MyFont2
} // compiler will call Dispose on MyFont and MyFont2
Font MyFont3 = new Font("Arial", 10.0f);
using (MyFont3)
{
// use MyFont3
} // compiler will call Dispose on MyFont3

}
}


2) GC.Collect()
Forces garbage collection.


using System;
namespace GCCollectIntExample
{
class MyGCCollectClass
{
private const long maxGarbage = 1000;

static void Main()
{
MyGCCollectClass myGCCol = new MyGCCollectClass();
// Determine the maximum number of generations the system
// garbage collector currently supports.
Console.WriteLine("The highest generation is {0}", GC.MaxGeneration);

myGCCol.MakeSomeGarbage();
// Determine which generation myGCCol object is stored in.
Console.WriteLine("Generation: {0}", GC.GetGeneration(myGCCol));

// Determine the best available approximation of the number
// of bytes currently allocated in managed memory.
Console.WriteLine("Total Memory: {0}", GC.GetTotalMemory(false));

// Perform a collection of generation 0 only.
GC.Collect(0);

// Determine which generation myGCCol object is stored in.
Console.WriteLine("Generation: {0}", GC.GetGeneration(myGCCol));

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

// Perform a collection of generation 2 only.
GC.Collect(2);

// Determine which generation myGCCol object is stored in.
Console.WriteLine("Generation: {0}", GC.GetGeneration(myGCCol));
Console.WriteLine("Total Memory: {0}", GC.GetTotalMemory(false));
Console.Read();
}
void MakeSomeGarbage()
{
Version vt;
for(int i = 0; i < maxGarbage; i++)
{
// Create objects and release them to fill up memory
// with unused objects.
vt = new Version();
}
}
}
}


Read help!

yemiu
November 24th, 2004, 01:23 AM
Thanks alot for your detailed answers. I fully understand now.
Pat

boudino
November 24th, 2004, 02:33 AM
+ Why Microsoft develops automated garbage collection instead of manual freeing up unused memory?

Because a programmer have not to worry about memory. He have to take care only about objects and sending messages among them.

yemiu
November 25th, 2004, 01:16 AM
Thanks alot. I understand now how the Garbage collection is useful.
I have a question here:
+ I am quite confused between different methods of freeing up unused memmory: IDispose interface, Dispose () method (sometimes we dont implement IDispose but still have to provide the Dispose() method?), Finalize() method, using() statement and destructor.
How they interract?
Which is compulsory, which is optional?
Thanks
Patrick

boudino
November 25th, 2004, 03:20 AM
Look e.g. at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/dotnetGCbasics.asp.

Mechanism of finalizing objects and freeing memory is not so easy to explain it myself. Take course of profesionals :)

yemiu
November 25th, 2004, 04:01 PM
Thank you
Patrick

darwen
November 25th, 2004, 04:28 PM
My article has now gone live : it should help resolve your problems.

It also has links to a couple of Garbage Collector resources.

http://www.codeguru.com/Csharp/Csharp/cs_syntax/interfaces/article.php/c8679/

Darwen.