Garbage Collection

J

John Jenkins

Hi,
I have been working on a small C# project. As part of my testing I have been profiling the memory usage, and instances of classes created. I have tried to be careful to dispose of any objects that use Unmanaged code. As an example I have been using the MemoryStream object, however when I use it I wrap its creation in the using statement

Class A
for(int i=0; i < 350; i++)

{

MemoryStreamCreator.CreateMemStreamManaged();

Thread.Sleep(500);

}



Class B

public static void CreateMemStreamManaged()

{


using(MemoryStream memStream = new MemoryStream(100))

{

Console.WriteLine("Created Memory Stream " + i);

}

++i;

}

When I call this static method from my main calling class I had expected to see in my profiler objects of type MemoryStream being created and destroyed dynamically. However what i observed was 200 MemoryStream objects being created then destroyed, then a further 150 being created. Is this how the GC works?



Thanks
 
N

Nicholas Paldino [.NET/C# MVP]

John,

Disposing of an object is very different from it being collected by the
GC. When you call the implementation of Dispose on an object that
implements IDisposable, you are really releasing any critical resources that
the object may have a hold of.

Most of the time, these objects are handles to things such as files, or
databases, but this doesn't always have to be the case.

When you call Dispose, it just means that those resources are released
in a proper manner. The object itself (which is usually a managed
representation), still exists, and will be collected when the GC does its
sweep.

What you are seeing is a GC kicking in when 200 objects are created
(which happens to be the threshold for your environment). This is fine.
The GC makes the determination when to perform itself, and as long as you
are calling Dispose, you can be assured that no critical resources are being
held longer than they have to be.

Also, for a MemoryStream, the call to Dispose doesn't actually dispose
of anything (the memory store for the stream is a managed byte array), so
calling Dispose won't do much of anything.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hi,
I have been working on a small C# project. As part of my testing I have
been profiling the memory usage, and instances of classes created. I have
tried to be careful to dispose of any objects that use Unmanaged code. As an
example I have been using the MemoryStream object, however when I use it I
wrap its creation in the using statement

Class A
for(int i=0; i < 350; i++)
{
MemoryStreamCreator.CreateMemStreamManaged();
Thread.Sleep(500);
}

Class B
public static void CreateMemStreamManaged()
{
using(MemoryStream memStream = new MemoryStream(100))
{
Console.WriteLine("Created Memory Stream " + i);
}
++i;
}
When I call this static method from my main calling class I had expected to
see in my profiler objects of type MemoryStream being created and destroyed
dynamically. However what i observed was 200 MemoryStream objects being
created then destroyed, then a further 150 being created. Is this how the GC
works?

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top