TWAIN Working Group

Newsletter Signup
Donate
Help keep TWAIN free
  • About TWAIN
    • What’s New?
    • News
    • Events
    • Membership
    • Consider a Donation
    • Contact Us
  • Why TWAIN?
  • Developers
    • Driver Developer
    • Application Developer
    • TWAIN Features
    • Specification & Tools
    • Self Certification Process
  • Support Forums
  • Scanner End-User
  • Find Certified Drivers
    • Facebook
    • LinkedIn
    • Vimeo

application memory and virtual memory increase quickly

Forums › TWAIN Classic › application memory and virtual memory increase quickly

  • This topic has 7 replies, 2 voices, and was last updated 13 years, 4 months ago by cmutex.
Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • September 20, 2007 at 5:59 am #22220 Reply
    cmutex
    Participant
    • Topics - 1
    • Replies - 3
    • Total Posts - 4

    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!

    September 20, 2007 at 2:21 pm #24140 Reply
    gabe
    Participant
    • Topics - 9
    • Replies - 583
    • Total Posts - 592

    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?

    .

    September 21, 2007 at 6:07 am #24141 Reply
    cmutex
    Participant
    • Topics - 1
    • Replies - 3
    • Total Posts - 4

    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;
    }

    September 21, 2007 at 7:21 am #24142 Reply
    gabe
    Participant
    • Topics - 9
    • Replies - 583
    • Total Posts - 592

    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.

    September 21, 2007 at 9:21 am #24143 Reply
    cmutex
    Participant
    • Topics - 1
    • Replies - 3
    • Total Posts - 4

    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;
    }

    September 21, 2007 at 9:46 am #24144 Reply
    gabe
    Participant
    • Topics - 9
    • Replies - 583
    • Total Posts - 592

    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?

    September 21, 2007 at 12:30 pm #24145 Reply
    gabe
    Participant
    • Topics - 9
    • Replies - 583
    • Total Posts - 592

    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.

    .

    September 24, 2007 at 2:29 am #24146 Reply
    cmutex
    Participant
    • Topics - 1
    • Replies - 3
    • Total Posts - 4

    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.

  • Author
    Posts
Viewing 8 posts - 1 through 8 (of 8 total)
Reply To: application memory and virtual memory increase quickly
Your information:




Quick Links

Service Providers
TWAIN Support Forums
Membership
Contact Us
Privacy Policy

Newsletter Signup

TWAIN Working Group Family

TWAIN Working Group
TWAIN Direct®
TWAIN Resources
TWAIN Certified Drivers
PDF/raster

  • Facebook
  • GitHub
  • LinkedIn
  • Vimeo

Recent Topics

  • EPSON V600 TWAIN and WIA on Windows 10
  • When and how to use WaitForEvents command ?
  • Problem enumerating list of installed scanners in windows server 2012
  • Failed to create TWAIN progress! Error code is 1260.
  • To get the list of scanners from javascript client side (browser)
  • Quarterly Newsletter
  • TWAIN Working Group Membership
  • Logo Usage
  • TWAIN License
  • Contact Us
Privacy Policy • Privacy Tools • Copyright © 2021 TWAIN Working Group • by iHwy, LLC • Log in

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.