none
Garbage Collector - Memory Usage RRS feed

  • Question

  • Hi,

    We are having some problems about memory usage on .NET. Even letting our app on stand by it keeps increasing memory usage by 8kb (not always). It wasnt a big deal so far, but since last week our app was expanded to a new client that had a worse CPU, so we really need to get it done.

    Does anyone knows something about GC? I try to call GC.Collect(), but looks like it doesnt make all the work watching by the TaskMgr.

    Our biggest problem happens when we create a new Form instance, closes and then creates a new one, cuz looks like that GC simply doesnt releases all the resources used by the 1st one.

    So far i created a timer that calls GC.Collect() from 15s to 15s, cuz its better than wait for GC think that its needed to run. Also we tried to make our Form inherits IDisposable and then calls this.Dispose on FormClosing event.

    Thats the approach we are using, but even when we call GC.Collect() along with Dispose(), it doesnt release all the resources

    Best regards,

    Felipe Momm
    Wednesday, October 1, 2008 10:27 PM

Answers

  • Hello Felipe,

    It's not advisable to invoke the GC explicitely, I mean using the GC.Collect. Your application should be suffering from memory leaks because of the controls not getting destroyed properly. According to my understanding GC.Collect() won't make much of difference. And by trying to explicitely invoke GC, you are even increasing the load on the CPU. Use any of the profilers and try to isolate which part of the application is consuming more memory. Try to identify whether you are using any unmanaged objects?; if so, are you destroying it immediately after usage?

    Otherwise it's not possible to predict the solution.

    Regards,

    Hemanth.


    Hemanth .NET Professional
    • Marked as answer by Zhi-Xin Ye Wednesday, October 8, 2008 9:47 AM
    Sunday, October 5, 2008 11:46 AM

All replies

  • .NET preallocates memory it might use in the future.  That doesn't necessary correspond to the actual memory usage of your process, much less the CPU usage (since you mentioned a "worse CPU").  You need Microsoft's CLR Profiler to determine the actual current memory usage of your .NET process.

    Form.Close should already call Dispose if Close is allowed to proceed, no need to call it manually.  How do you know that the form's resources are not released, anyway?  Did you run a Win32/GDI resource monitoring tool?

    Calling GC.Collect on a timer is completely pointless and definitely not smarter than let the CLR decide when to run GC.  Stop doing that, you're just slowing down the program.

    Again, how do you know that resources are not released?  And even assuming that this is the case, how do you know that your performance issues would be solved if they were?  Maybe your client's slower CPU is just... slower?

    Thursday, October 2, 2008 7:32 AM
  • If you suspect you have a memory leak, use a tool like red-gate's ants profiler to track down any leaks, if you don't have the budget for a profiler microsoft actually has some decent (But not that easy to use) tools to track down leaks, Check this blog on some pointers on how to do that.
    Thursday, October 2, 2008 1:57 PM
  • Hello Felipe,

    What version of .NET are you using?

    Make sure you build your project in Release mode - if you do a search on 'memory leak' in this forum, you might find some related posts.

    good luck
    Thursday, October 2, 2008 6:37 PM
  • Im using .NET 3.5 Framework.

    I will search for memory leak to find if i have an answer.

    Felipe Momm
    Friday, October 3, 2008 11:47 AM
  • If you dynamically creates some controls for your form which were not added with Designer, you need to assign them null pointer value explicitly. Otherwise this memory won't be free even if you close parent form.
    Friday, October 3, 2008 3:08 PM
  • Im checking it through TaskManager and Immediate Window on Visual Studio using Son of Strike.

    I will verify if we are creating some controls dynamically, in fact we are, but i dont know how many.

    Felipe Momm
    Friday, October 3, 2008 4:54 PM
  • Well, our apps run on a Terminal Server, so we have a lot of clients running the same app on this server and its throwing some Memory Exceptions when we have more than 20 clients on this server.

    I think what leads to this is the Free resources (watched via SOS.dll) that Garbage Collector doesnt free, it only set as free, but continue to keep this memory on the app for a further use even if we are needing more memory in others app on the same CPU.

    What do you think?

    Felipe Momm
    Friday, October 3, 2008 11:58 PM
  • Hello Felipe,

    It's not advisable to invoke the GC explicitely, I mean using the GC.Collect. Your application should be suffering from memory leaks because of the controls not getting destroyed properly. According to my understanding GC.Collect() won't make much of difference. And by trying to explicitely invoke GC, you are even increasing the load on the CPU. Use any of the profilers and try to isolate which part of the application is consuming more memory. Try to identify whether you are using any unmanaged objects?; if so, are you destroying it immediately after usage?

    Otherwise it's not possible to predict the solution.

    Regards,

    Hemanth.


    Hemanth .NET Professional
    • Marked as answer by Zhi-Xin Ye Wednesday, October 8, 2008 9:47 AM
    Sunday, October 5, 2008 11:46 AM