R
roger.dunham
I am trying to identify whether a .NET 1.1 application that I have
written has a memory leak. I thought I understood how .NET memory
management worked, but it appears that there is more to it than I
realised.
I have created a simple app that creates a byte array which is stored
as a member of the application's main form.
The important code is shown below.
private byte[] m_ar; //byte array to store data
private void btnCreateArray_Click(object sender, System.EventArgs e)
{
int nSize = Convert.ToInt32( this.txtBufferSize.Text);
byte[] buffer = new byte[nSize];
m_ar = buffer;
this.Text = "Grabbed memory";
}
//set the member array to null to allow it to be garbage collected, //
then force a garbage collection.
private void btnReleaseArray_Click(object sender, System.EventArgs e)
{
m_ar = null;
System.GC.Collect();
this.Text = "Released memory";
}
//Force a garbage collection
private void btnCollectGarbage_Click(object sender, System.EventArgs
e)
{
GC.Collect();
}
The user enters a value into the text box txtBufferSize (A typical
test value is 50,000,000).
In Task Manager the amount of virtual memory used increases by about
50MB, which is exactly as you might expect.
However the amount of "Committed Bytes" as shown by the Performance
Monitor remains at a few 100K.
If I then force a garbage collection, then at that point, the number
of committed bytes increases to around 50MB. Task manager continues to
indicate that virtual memory is around 50MB.
My questions are:
Why does it take a garbage collection for the application to "commit"
the bytes?
Where is the memory that is being used before it appears as committed
bytes?
written has a memory leak. I thought I understood how .NET memory
management worked, but it appears that there is more to it than I
realised.
I have created a simple app that creates a byte array which is stored
as a member of the application's main form.
The important code is shown below.
private byte[] m_ar; //byte array to store data
private void btnCreateArray_Click(object sender, System.EventArgs e)
{
int nSize = Convert.ToInt32( this.txtBufferSize.Text);
byte[] buffer = new byte[nSize];
m_ar = buffer;
this.Text = "Grabbed memory";
}
//set the member array to null to allow it to be garbage collected, //
then force a garbage collection.
private void btnReleaseArray_Click(object sender, System.EventArgs e)
{
m_ar = null;
System.GC.Collect();
this.Text = "Released memory";
}
//Force a garbage collection
private void btnCollectGarbage_Click(object sender, System.EventArgs
e)
{
GC.Collect();
}
The user enters a value into the text box txtBufferSize (A typical
test value is 50,000,000).
In Task Manager the amount of virtual memory used increases by about
50MB, which is exactly as you might expect.
However the amount of "Committed Bytes" as shown by the Performance
Monitor remains at a few 100K.
If I then force a garbage collection, then at that point, the number
of committed bytes increases to around 50MB. Task manager continues to
indicate that virtual memory is around 50MB.
My questions are:
Why does it take a garbage collection for the application to "commit"
the bytes?
Where is the memory that is being used before it appears as committed
bytes?