locked
Strange Memory Usage? ...or Normal? RRS feed

  • Question

  • I have a console application that just sleeps for one minute and then sleeps again for one minute, repeat, repeat; there's not any output to the console or anything, it just sleeps over and over again.  I build a release of this and it seems that memory usage continues to go up...at 11:44 the task manager showed memory usage at 6216 and at 12:10 the memory usage had went up to 6520.

    This is a really basic program, but I cannot figure out why this is using memory like it is.

    <PROGRAM_START>
    using System;

    namespace TestProject
    {
        class Program
        {
            public static bool stop = false;
            private static int OFFSET = 30000;
            private static int INTERVAL = 60000;
            private static int delay;

            static void Main(string[] args)
            {
                try
                {
                    while (!stop)
                    {
                        delay = ((INTERVAL + OFFSET) - (DateTime.Now.Second * 1000 + DateTime.Now.Millisecond)) % INTERVAL;
                        System.Threading.Thread.Sleep(delay);
                    }
                }
                catch (Exception ex)
                { }
            }
        }
    }
    <PROGRAM_END>

    EDIT:
    C# VS2008 .NET 2.0

    Tuesday, January 19, 2010 6:18 PM

Answers

  • Hi HMote,

    I test your code and monitor the memory of the application, its memory usage doesn't go up, here is my test result:

    Just after it start: 4736
    after a while:      4672

     I can't reproduce it. Here is a tool, it can help you find why the memory go up, you can try it:
    .NET Memory Profiler


    Best regards,
    Guang-Ming Bian - MSFT
    MSDN Subscriber Support
    in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com
    Please remember to mark the replies as answers if they help and unmark them if they provide no help
    Wednesday, January 20, 2010 6:45 AM
  • Your application's memory usage keeps growing because it's .net :). You should read more about how the automatic memory management in .net works. Basically when you call something like


    delay = ((INTERVAL + OFFSET) - (DateTime.Now.Second * 1000 + DateTime.Now.Millisecond)) % INTERVAL;


    you are creating a new object with the call to DateTime.Now (so on this line probably two new objects) and they need some memory. Also you can't know how many new objects will the DateTime.Now call create internally, how many objects will the System.Threading.Thread.Sleep call create internally, etc etc... Now the point is that the .net runtime will take care of all this and remove any unused objects from memory when it feels it's the right time. You can force this to happen immediately by calling GC.Collect(). Now since your application is only using 6 MB of memory and your system probably still has plenty of free memory the .net runtime will not think it's necessary to do any garbage collection anytime soon. If your application was using more memory or your system was more memory taxed the GC would collect unused objects sooner.
    • Marked as answer by HMote Thursday, January 21, 2010 5:44 PM
    Thursday, January 21, 2010 1:38 PM

All replies

  • on the while loop try to force the garbage collector to do some work... maybe it helps... but why the mem usage is growing... i really dont know... maybe ur just looking when it spikes...

    Tuesday, January 19, 2010 6:51 PM
  • It is still currently 6520, so I assume that initially it just varies until it has the memory it needs?  I don't know.  I guess I didn't watch it for long enough, initially.  I checked it a couple times when I first started:

    11:44 = 6216
    11:47 = 6296
    11:50 = 6360
    12:10 = 6520
    12:57 (Current) = 6520

    Tuesday, January 19, 2010 6:57 PM
  • How do I "force the garbage collector to do some work"?  I think I would like to implement that in my finished code since it is a continuous loop.
    Tuesday, January 19, 2010 7:27 PM
  • Hi HMote,

    I test your code and monitor the memory of the application, its memory usage doesn't go up, here is my test result:

    Just after it start: 4736
    after a while:      4672

     I can't reproduce it. Here is a tool, it can help you find why the memory go up, you can try it:
    .NET Memory Profiler


    Best regards,
    Guang-Ming Bian - MSFT
    MSDN Subscriber Support
    in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com
    Please remember to mark the replies as answers if they help and unmark them if they provide no help
    Wednesday, January 20, 2010 6:45 AM
  • Hi,

    You can force the Garbage Collection (GC) by invoking the "Collect" method,
    like this: 
      

          GC.Collect();

    Some useful links:
    Microsoft have also provided a Memory analyzer (The CRL Profiler)
    http://www.microsoft.com/downloads/details.aspx?familyid=86ce6052-d7f4-4aeb-9b7a-94635beebdda&displaylang=en (Download)
    http://www.microsoft.com/downloads/details.aspx?FamilyId=CD5AA57C-9CD1-45AB-AA4B-8DC586D30938&displaylang=en (Introduction to CRL Profiler Download from Microsoft)
    http://msdn.microsoft.com/en-us/library/ms979205.aspx (How To: User CRL Profiler).

    I hope this helps...

    Have a nice day...

    Best regards,
    Fisnik


    Coder24.com
    Wednesday, January 20, 2010 7:11 AM
  • Hi HMote,

    I test your code and monitor the memory of the application, its memory usage doesn't go up, here is my test result:

    Just after it start: 4736
    after a while:      4672

     I can't reproduce it. Here is a tool, it can help you find why the memory go up, you can try it:
    .NET Memory Profiler


    Best regards,
    Guang-Ming Bian - MSFT
    MSDN Subscriber Support
    in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com
    Please remember to mark the replies as answers if they help and unmark them if they provide no help

    Sorry pressed wrong BUTTON!
    Coder24.com
    Thursday, January 21, 2010 12:24 PM
  • Your application's memory usage keeps growing because it's .net :). You should read more about how the automatic memory management in .net works. Basically when you call something like


    delay = ((INTERVAL + OFFSET) - (DateTime.Now.Second * 1000 + DateTime.Now.Millisecond)) % INTERVAL;


    you are creating a new object with the call to DateTime.Now (so on this line probably two new objects) and they need some memory. Also you can't know how many new objects will the DateTime.Now call create internally, how many objects will the System.Threading.Thread.Sleep call create internally, etc etc... Now the point is that the .net runtime will take care of all this and remove any unused objects from memory when it feels it's the right time. You can force this to happen immediately by calling GC.Collect(). Now since your application is only using 6 MB of memory and your system probably still has plenty of free memory the .net runtime will not think it's necessary to do any garbage collection anytime soon. If your application was using more memory or your system was more memory taxed the GC would collect unused objects sooner.
    • Marked as answer by HMote Thursday, January 21, 2010 5:44 PM
    Thursday, January 21, 2010 1:38 PM
  • One other tool is to use Perfmon and look at Private Bytes telltale sign of a memory leak. See this article Identify And Prevent Memory Leaks In Managed Code to begin that process.


    In general when the OS runs an application it tries to determine how much memory is being used. It will allocate more memory for the application that what the application currently needs. The reason for that is, to allocate/deallocate memory is a cpu intensive operation. Why parallel what the app needs when it can reside in a pool of memory which will allow it, the app, to expand and contract with no interference from the operating system. Saves on cycles.

    What the developer sees is a high water mark for memory. If the system is not stressed, the system will not reclaim any memory and keep the high water mark. There are many posts in this forum where the users say, processing done, memory cleaned up but the OS still shows my app at memory point X, when it should be memory point M (lower). Winform users report the same but if one minimizes the app, suddenly the mem usage reported drops to that M level. That is done by design. Minimizing suggests that an app doesn't need the memory for it will not be interacting with the user and the OS will reclaim the point. If its not a winform and the OS is not stressed, the app stays at the high water mark of X.


    More articles of interest:

    CLR Inside Out: Investigating Memory Issues -- MSDN Magazine, November 2006
    Debug Leaky Apps: Identify And Prevent Memory Leaks In Managed Code -- MSDN Magazine, January 2007
    VMMap which is a process virtual and physical memory analysis utility,
    Download details: Debug Diagnostic Tool v1.1
    Joe Duffy's Weblog (Dispose, Finalization, and Resource Management)

    HTH GL
    William Wegerson (www.OmegaCoder.Com)
    Thursday, January 21, 2010 1:50 PM
  • Just as an aside it may be worth considering changing over the application to be a windows service which can then be set to start automatically when windows starts, rather than relying on a console application which means that you need to be logged into the machine - only really applies if your application is going to be running unattended for long periods of time.
    Thursday, January 21, 2010 1:57 PM
  • It will be a service when I'm done, it was just easier to test it in a console application...and then I found the memory usage interesting.  Thanks for all the help guys, I definately learned some things.  In the end, I think it was a problem with an API that I received from one of the companies I'm working with.  I did some tests on some of their methods and compared to the memory usage of the DateTime object, the differences are massive.  I'm in contact with this company currently and will definately come back for more help if needed.  Thanks for all the help!
    Thursday, January 21, 2010 5:43 PM