|
|
Rank: Member Groups: Member
Joined: 4/14/2005 Posts: 4 Points: 0
|
I have noticed some memory leaks in the Bitmap/BitmapData classes when I call them from a thread other than the UI thread. Both are documented as thread safe. You can create a test case by adding the code snippets below to the Main demo. Monitoring the process' memory usage via Task Manager shows evidence of the leak. Bill Code:
private System.Threading.AutoResetEvent testThreadBeginWorkEvent = new AutoResetEvent(false);
private System.Threading.AutoResetEvent testThreadWorkDoneEvent = new AutoResetEvent(false);
Thread testThread;
private void testThreadProc()
{
try
{
while (true)
{
testThreadBeginWorkEvent.WaitOne();
using (Aurigma.GraphicsMill.BitmapData bitmapData = bitmapViewerMainView.Bitmap.LockBits())
{
IntPtr bitmapPtr = bitmapData.Scan0;
for (int row = 0; row < bitmapViewerMainView.Bitmap.Height; row++)
{
bitmapPtr = (IntPtr)((int)bitmapPtr + bitmapData.Stride);
}
bitmapViewerMainView.Bitmap.UnlockBits(bitmapData);
}
testThreadWorkDoneEvent.Set();
}
}
catch
{
}
}
private void FormMain_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
testThread.Abort();
testThread.Join();
}
if (testThread == null)
{
testThread = new Thread(new ThreadStart(testThreadProc));
testThread.Start();
}
for (int i=0; i<1000; i++)
{
testThreadBeginWorkEvent.Set();
testThreadWorkDoneEvent.WaitOne();
}
return;
|
|
Rank: Advanced Member Groups: Administration
, Member
Joined: 8/3/2003 Posts: 471 Points: 243
|
Hello, If you modify your thead procedure as following: Code:
private void testThreadProc()
{
try
{
while (true)
{
testThreadBeginWorkEvent.WaitOne();
using(Aurigma.GraphicsMill.BitmapData bitmapData = bitmapViewerMainView.Bitmap.LockBits())
{
IntPtr bitmapPtr = bitmapData.Scan0;
for (int row = 0; row < bitmapViewerMainView.Bitmap.Height; row++)
{
bitmapPtr = (IntPtr)((int)bitmapPtr + bitmapData.Stride);
}
bitmapViewerMainView.Bitmap.UnlockBits(bitmapData);
}
System.GC.Collect(System.GC.MaxGeneration);
testThreadWorkDoneEvent.Set();
}
}
catch
{
}
}
memory usage will be reduced. The method System.GC.Collect forces execution of system garbage collector. This demonstrates that all objects are freed correctly but garbage collector "makes a decision" to collect unnecessary objects to free them later.
Sincerely yours, Dmitry Sevostjanov.
|
|
Rank: Member Groups: Member
Joined: 4/14/2005 Posts: 4 Points: 0
|
I tried this and it does not seem to make any difference. Did you modify the test case and see a difference? If so, any idea what is going on?
Bill
|
|
Rank: Advanced Member Groups: Administration
, Member
Joined: 8/3/2003 Posts: 471 Points: 243
|
I created .NET windows application which contains only your code and inserted the call System.GC.Collect method. No memory leaks was detected. I use perfomance monitor with Working Set counter to detect memory usage.
Could you send us your complete test application and descibe how you measure memory leaks? Also how large memory leaks do you get?
Sincerely yours, Dmitry Sevostjanov.
|
|
|
Guest |