Home › Forums › TWAIN Classic › application memory and virtual memory increase quickly
- This topic has 7 replies, 2 voices, and was last updated 16 years, 6 months ago by cmutex.
- AuthorPosts
System Operation: WindowXP+SP2
Call Twain interface by C#,When scan 500 pictures continual, the application memory from task manager is from 240M increase to 612M, the application virtual memory from 230M increase to 1800M. Why? how can i control the memory and virtual memory increase. thanks every one!
your code leaks. If I were to guess (and I sorta have to without some code to look at) I’d say you’re using native transfer mode and leaking image handles.
Are you using GlobalFree on the intptr that you pass to Image.ImageNativeXfer.Get after you’re done with them?
.
Hi Gabe, thanks for your reply, yes, but i has use GlobalFree to release the handle, see my code segment. thanks again!
bool IMessageFilter.PreFilterMessage(ref Message m)
{
TwainCommand cmd = TwianScanner.PassMessage(ref m);
if (cmd == TwainCommand.Not)
return false;switch (cmd)
{
case TwainCommand.CloseRequest:
{
EndingScan();
TwianScanner.CloseSrc();
break;
}
case TwainCommand.CloseOk:
{
EndingScan();
TwianScanner.CloseSrc();
break;
}
case TwainCommand.DeviceEvent:
{
break;
}
case TwainCommand.TransferReady:
{ArrayList pics = TwianScanner.TransferPictures();
this.AutoScrollPosition = new Point(0, 0);
EndingScan();TwianScanner.CloseSrc();
m_nPicnumber++;
for (int i = 0; i < pics.Count; i++)
{
IntPtr imgPtr = (IntPtr)pics;IntPtr bmpptr = GlobalLock(imgPtr);
IntPtr pixptr = GetPixelInfo(bmpptr);this.AutoScrollPosition = new Point(0, 0);
if (m_bmpOrigin!=null)
{
m_bmpOrigin.Dispose();
}m_bmpOrigin = InptrToBitmap(pixptr, bmpptr);
Bitmap bmp = new Bitmap(m_bmpOrigin);if (m_bFullSize)
{
pictureBox.Width = bmp.Width;
pictureBox.Height = bmp.Height;}
else
{
pictureBox.Width = m_nPicOrgWidth;
pictureBox.Height = m_nPicOrgHeight;
bmp = new Bitmap(m_bmpOrigin, this.pictureBox.Width, this.pictureBox.Height);}
m_nRotate = 1;
pictureBox.Image = bmp;
if (imgPtr != null)
{
GlobalFree(imgPtr);
}if (bmpptr!=null)
{
GlobalFree(bmpptr);
}if (pixptr!=null)
{
GlobalFree(pixptr);
}}
if (pics!=null)
{
pics.Clear();}
break;
}
}return true;
}InptrToBitmap, are you using the SetDIBitsToDevice method? – are you disposing the graphics object? Otherwise you code is similar to how I obtain images in .net, but my code rarely jumps to 600meg.
Hi Geba, yes,i using the SetDIBitsToDevice method,but has release the object as coding segement:
protected Bitmap InptrToBitmap(IntPtr pixptr, IntPtr bmpptr)
{
Bitmap bitmap=new Bitmap(bmprect.Width,bmprect.Height);
using (Graphics g = Graphics.FromImage(bitmap))
{IntPtr hdc = g.GetHdc();
SetDIBitsToDevice(hdc, bmprect.X, bmprect.Y, bmprect.Width, bmprect.Height,
bmprect.X, bmprect.Y, 0, bmprect.Height, pixptr, bmpptr, 0);
g.ReleaseHdc(hdc);
}
return bitmap;
}it’s been a little while since I worked on this bit of code, but I see one other difference between my code and yours – I call GlobalUnLock on imgPtr just before I call GlobalFree. I also don’t put my GlobalFree calls in an if block but I don’t think that is the issue. If you add GlobalUnLock before GlbalFree does it change your memory usage?
background thoughts, these numbers will be somewhat relative to the memory I have so for reference I’ve got 1Gb allocated to the vmware dev machine running xp sp2 using vs2005 sp1.
I rechecked my native acquisitions and my memory usage does baloon when running from a winforms project, ~400-500Mb peeks memory usage with the app showing around 200Mb in use after I have acquired an image even if i’m not doing anything and as long as that winforms project is the active window (minimize it and it drops to 4Mb, but thats the framework for you). File transfers peek somewhere in the 200-250Mb range with the same memory use when idle after an image has been acquired. Memory usage doesn’t seem to increase with every acquisition, after I take the hit on the first acquisition it’s pretty stable.
the same base library running under a windows service (which btw I’m just tickled with) idles around 20Mb with peek memory usage around 80Mb.
If you put your twain logic in a dll and drive it from a console project that saves the images to a file instead of loadig them to control on a winform, what does your memory usage look like?
also, if you find a solution it’s worth at least a beer to me if you post it to me somehow.
.
Hi Gabe, thanks very much, if i scan step by step handwork(the scan speed is slowly), the virtual memory and memory is released delay some minutes, i think may be the code is not any bug, if i scan by TestParter(testing tool, running script, scan speed is higher), the virtual memory and memory used speed higher than release speed, so appear phenomena memory and virtual memory more and more.
- AuthorPosts