Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Detect cause of application memory leak

Detect cause of application memory leak

Scheduled Pinned Locked Moved C#
csharpperformancequestion
10 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Andy Davey
    wrote on last edited by
    #1

    Hi, I've written this windows service in C# which unfortunately has a memory leak in it. Basically the program starts out using a 10Mb working set, but after three days it goes up to 200Mb, which says to me somewhere I'm forgetting to dereference some objects etc. The thing is I've looked and I can't find it. Is there a tool or methodology for finding object references that are being held onto erroneously by my application? Shouldn't be too hard to find - theres 200Mb of them (and thats after 3 days - this thing is supposed to run 24x7) TIA, Andy

    J J M D L 5 Replies Last reply
    0
    • A Andy Davey

      Hi, I've written this windows service in C# which unfortunately has a memory leak in it. Basically the program starts out using a 10Mb working set, but after three days it goes up to 200Mb, which says to me somewhere I'm forgetting to dereference some objects etc. The thing is I've looked and I can't find it. Is there a tool or methodology for finding object references that are being held onto erroneously by my application? Shouldn't be too hard to find - theres 200Mb of them (and thats after 3 days - this thing is supposed to run 24x7) TIA, Andy

      J Offline
      J Offline
      James Simpson
      wrote on last edited by
      #2

      make sure you "Dispose" all objects which can be disposed! setting the references to null wont nessaccarily kill the objects, the garbage collection in .NET removes dead objects periodically (from what I have seen) Typically i have seen my applications the memory useage gradually goes up then every so often it returns to a normal level, this is probably when garbage collection kicks in. I follow these rules: 1) Any objects which need to be closed get closed in a finally block 2) Any objects which need to be Disposed get disposed and set to null Cheers, James James Simpson Web Developer imebgo@hotmail.com

      1 Reply Last reply
      0
      • A Andy Davey

        Hi, I've written this windows service in C# which unfortunately has a memory leak in it. Basically the program starts out using a 10Mb working set, but after three days it goes up to 200Mb, which says to me somewhere I'm forgetting to dereference some objects etc. The thing is I've looked and I can't find it. Is there a tool or methodology for finding object references that are being held onto erroneously by my application? Shouldn't be too hard to find - theres 200Mb of them (and thats after 3 days - this thing is supposed to run 24x7) TIA, Andy

        J Offline
        J Offline
        Julian Bucknall MSFT
        wrote on last edited by
        #3

        Search for "Application Profiler" on http://www.gotdotnet.com/community/usersamples/. It'll enable you to see what's going on with your managed heap. There are other similar tools out there; try googling for "managed heap profiler" or similar. Cheers, Julian Program Manager, C# This posting is provided "AS IS" with no warranties, and confers no rights.

        1 Reply Last reply
        0
        • A Andy Davey

          Hi, I've written this windows service in C# which unfortunately has a memory leak in it. Basically the program starts out using a 10Mb working set, but after three days it goes up to 200Mb, which says to me somewhere I'm forgetting to dereference some objects etc. The thing is I've looked and I can't find it. Is there a tool or methodology for finding object references that are being held onto erroneously by my application? Shouldn't be too hard to find - theres 200Mb of them (and thats after 3 days - this thing is supposed to run 24x7) TIA, Andy

          M Offline
          M Offline
          MtnBiknGuy
          wrote on last edited by
          #4

          I see that the word "defererenced" has taken on a whole new meaning in C#! This is very funny! :omg:

          1 Reply Last reply
          0
          • A Andy Davey

            Hi, I've written this windows service in C# which unfortunately has a memory leak in it. Basically the program starts out using a 10Mb working set, but after three days it goes up to 200Mb, which says to me somewhere I'm forgetting to dereference some objects etc. The thing is I've looked and I can't find it. Is there a tool or methodology for finding object references that are being held onto erroneously by my application? Shouldn't be too hard to find - theres 200Mb of them (and thats after 3 days - this thing is supposed to run 24x7) TIA, Andy

            D Offline
            D Offline
            Daniel Turini
            wrote on last edited by
            #5

            Hmmm... The most common cause of memory leaks on .NET apps I see is with COM/COM+ interop code. Most people don't know/remember to call System.Runtime.InteropServices.Marshal.ReleaseComObject on all COM objects referenced.


            Help me dominate the world - click this link and my army will grow

            1 Reply Last reply
            0
            • A Andy Davey

              Hi, I've written this windows service in C# which unfortunately has a memory leak in it. Basically the program starts out using a 10Mb working set, but after three days it goes up to 200Mb, which says to me somewhere I'm forgetting to dereference some objects etc. The thing is I've looked and I can't find it. Is there a tool or methodology for finding object references that are being held onto erroneously by my application? Shouldn't be too hard to find - theres 200Mb of them (and thats after 3 days - this thing is supposed to run 24x7) TIA, Andy

              L Offline
              L Offline
              leppie
              wrote on last edited by
              #6

              More details...plz :) leppie::AllocCPArticle("Zee blog");
              Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it.

              A 1 Reply Last reply
              0
              • L leppie

                More details...plz :) leppie::AllocCPArticle("Zee blog");
                Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it.

                A Offline
                A Offline
                Andy Davey
                wrote on last edited by
                #7

                Basically my windows service is used to handle a teletext signal reader (essentially receives text over TV broadcasting) via a C api compiled to a dll. My service continually waits on the feed for new messages. Once a message is received, it is processed, stored to a database and made available for other applications in my domain. Where I initially thought my memory was getting gobbled up was in my queues used to pass messages between threads, since depending on the throughput of the device, could have up to 18,000 messages (each message object is probably < 100 bytes or so) waiting to be processed. But I did the math and that didn't come close to my 200Mb working set. Essentially one thread puts messages in a queue and another thread waits for messages in the queue, dequeues the message and processes it. There are around 10 such threading setups in the service. So I'm looking else where for the problem. Somewhere I'm forgetting to deallocate objects. One thing that I'm testing for at the moment is that maybe one of my threads is crashing and the queue of which its supposed to dequeue is filling up. Since the problem doesn't appear until the service has been running longer than 6 hours or more, it's going to take some time to find (which further makes me think that a thread is crashing somewhere). Failing one of my threads crashing over the weekend I will be looking at that manager heap profiler mentioned above in one of the posts. Andy

                L 1 Reply Last reply
                0
                • A Andy Davey

                  Basically my windows service is used to handle a teletext signal reader (essentially receives text over TV broadcasting) via a C api compiled to a dll. My service continually waits on the feed for new messages. Once a message is received, it is processed, stored to a database and made available for other applications in my domain. Where I initially thought my memory was getting gobbled up was in my queues used to pass messages between threads, since depending on the throughput of the device, could have up to 18,000 messages (each message object is probably < 100 bytes or so) waiting to be processed. But I did the math and that didn't come close to my 200Mb working set. Essentially one thread puts messages in a queue and another thread waits for messages in the queue, dequeues the message and processes it. There are around 10 such threading setups in the service. So I'm looking else where for the problem. Somewhere I'm forgetting to deallocate objects. One thing that I'm testing for at the moment is that maybe one of my threads is crashing and the queue of which its supposed to dequeue is filling up. Since the problem doesn't appear until the service has been running longer than 6 hours or more, it's going to take some time to find (which further makes me think that a thread is crashing somewhere). Failing one of my threads crashing over the weekend I will be looking at that manager heap profiler mentioned above in one of the posts. Andy

                  L Offline
                  L Offline
                  leppie
                  wrote on last edited by
                  #8

                  Thanx, this explains alot more :) Andy Davey wrote: Basically my windows service is used to handle a teletext signal reader (essentially receives text over TV broadcasting) via a C api compiled to a dll. You seem to constantly marshalling string data to the C# service (as I understand it), are the data being freed afterwards? .Net wont do this for you. It will make yet another copy of it, but managed this time (no worries now). Also the problem could be in the C code or the way the function being called is meant to implemeted, like said above whether the resulting char* should be freed or not. If you have the C code, you can easily "remedy" this by using a GC (i suggest libGC). HTH :) leppie::AllocCPArticle("Zee blog");
                  Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it.

                  A 1 Reply Last reply
                  0
                  • L leppie

                    Thanx, this explains alot more :) Andy Davey wrote: Basically my windows service is used to handle a teletext signal reader (essentially receives text over TV broadcasting) via a C api compiled to a dll. You seem to constantly marshalling string data to the C# service (as I understand it), are the data being freed afterwards? .Net wont do this for you. It will make yet another copy of it, but managed this time (no worries now). Also the problem could be in the C code or the way the function being called is meant to implemeted, like said above whether the resulting char* should be freed or not. If you have the C code, you can easily "remedy" this by using a GC (i suggest libGC). HTH :) leppie::AllocCPArticle("Zee blog");
                    Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it.

                    A Offline
                    A Offline
                    Andy Davey
                    wrote on last edited by
                    #9

                    Rrrrrright. I'd never considered that. Yeah the way that I access the API is by passing an uninitialised object which the API initialises (eg GetMesssage(out p_messageDetails);) Now who's repsonsibility is it to deallocate the memory used in the message? Mine or the marshallers? I wouldn't even know how to deallocate that memory to be honest :-). Its amazing how lazy you get when you code under a GC. Andy

                    L 1 Reply Last reply
                    0
                    • A Andy Davey

                      Rrrrrright. I'd never considered that. Yeah the way that I access the API is by passing an uninitialised object which the API initialises (eg GetMesssage(out p_messageDetails);) Now who's repsonsibility is it to deallocate the memory used in the message? Mine or the marshallers? I wouldn't even know how to deallocate that memory to be honest :-). Its amazing how lazy you get when you code under a GC. Andy

                      L Offline
                      L Offline
                      leppie
                      wrote on last edited by
                      #10

                      It sounds like your job :) You can try use: Marshal.FreeHGlobal() , but somehow i dont think that will cut it. Best approach would be to store the returned pointer, then do a manual marshall to with PtrToStructure, then use the above method to free it. Good luck :) leppie::AllocCPArticle("Zee blog");
                      Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups