Murat
Murat

Reputation: 11

My Program get performance boost with EmptyWorkingSet function? Is it dangerous?

We are developing Automation Software for Restaurants(using VS.NET C#). And we are using POS products which mostf of them has celeron or atom processor with 512 mb or 1gb ram on it. So at the begining know that we can't get more powerfull PC(i saw suggestion like "Get a better PC to your customer" :) ).

We have a memory problem. To represent the cart of a real customer, we use panel, we add custom control to represent product in cart. These means lots of controls are adding to panel, then removing(Panel.controls.Clear function.) This method causes a memory leak and program goes slow performance. After searching and googling, i found that Panel.Control.Clear() funtction clears all the control in the panel but not from the memory. So i disabled the panel to check, and wholaaa everything workss fine. No memory leaks anymore. So i decided to remove that cart panel to save memor. But later i found very usefull suggestions here, like EmptyWorkingSet.

    [DllImport("psapi.dll")]
    static extern int EmptyWorkingSet(IntPtr hwProc);

    static void MinimizeFootprint()
    {
        EmptyWorkingSet(Process.GetCurrentProcess().Handle);
    }

Then i added the code , enabled the cart panel, and it nothing changed. Performance is still good.No memory leak, no performance decreased. But some says in here and here EmptyWorkingSet may causes Page Faults and it is dangerous. I'am testing program and nothing happened. It works like a charm. So these are my options

  1. Remove the cart panel that causes memory problems.
  2. After clearing custom controls from cartPanel , remove the custom controls from the memory( i don't know how but i can figure it out)
  3. USE EmptyWorkingSet but it may cause page faults(truely i don't know what is that Page Fault mean)

SO WHAT DO YOU SAY

Upvotes: 1

Views: 2605

Answers (1)

Andy
Andy

Reputation: 8562

I think the problem is that you're clearing the collection of controls, when you should be disposing them. Also, be sure you're unhooking any event handlers to the the controls before you clear them as well, as event handlers setup references between objects, so the listener of the event will cause an otherwise eligible for collection object to stick around. I'd go that route before trying to force the working set to be cleared.

Upvotes: 1

Related Questions