1

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

Community
  • 1
  • 1
Murat
  • 11
  • 1
  • 3

1 Answers1

1

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.

Andy
  • 8,432
  • 6
  • 38
  • 76
  • i found great article about unhooking event handlers in here http://www.simple-talk.com/dotnet/.net-tools/tracing-memory-leaks-in-.net-applications-with-ants-profiler/ . This is a way disposing custom controls and unhooking events. But too much effort, why not to use EmptyWorkingSet just 2 lines of code. – Murat Apr 21 '11 at 17:31
  • @Murat: Its good practice to get in the habit of disposing objects which iimplement IDisposable. In this case what you're doing might work (and I doubt that), but in the future it likely won't. It's best not to screw around with how .Net garbage collection and IDisposable are intended to work. What you're doing is a hack, and will invariably cause you problems down the road. – Andy Apr 21 '11 at 22:17