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. Window Service memory leak

Window Service memory leak

Scheduled Pinned Locked Moved C#
helpsysadminperformancetutorial
8 Posts 3 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.
  • H Offline
    H Offline
    himanshu2561
    wrote on last edited by
    #1

    Hi i have number of window services running on production server(Window server 2003 dedicated for window services).Everything was running fine from last 6 months but now some services start giving error System.OutOfMemoryException. I think this issue is due to memory leaks.Now my questions are (1)For immediate solution restarting of window services is enough or i have to restart the Production server. (2)How to find the possible cause of memory leak and suggestion for possible causes of memory leak

    himanshu

    L P 2 Replies Last reply
    0
    • H himanshu2561

      Hi i have number of window services running on production server(Window server 2003 dedicated for window services).Everything was running fine from last 6 months but now some services start giving error System.OutOfMemoryException. I think this issue is due to memory leaks.Now my questions are (1)For immediate solution restarting of window services is enough or i have to restart the Production server. (2)How to find the possible cause of memory leak and suggestion for possible causes of memory leak

      himanshu

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, there are many possible causes. Here is one most people aren't aware of: objects larger than 80KB get allocated on the "large-object-heap", which never gets compacted. The net result is it may fragment after some time, resulting in lots of memory being free yet the heap being unable to offer a chunk of memory large enough to satisfy the latest request. Here are some candidates for large objects: - images - arrays in general - large strings (TextBox/RichTextBox often is a culprit, as it requires the concatenation of all its text) - StringBuillders and all kinds of collections since these typically are implemented as arrays as well; such objects get an initial capacity, and when insufficient, their array gets reallocated with twice the size. Solution: there isn't a perfect solution. Some suggestions: - avoid large objects as much as possible, don't use (Rich)TextBox for texts exceeding a few hundred lines - when a StringBuilder or collection will grow large, allocate it with sufficient size right away. - periodically restart your app (once a month, once a day, depending on how active its object allocation is) BTW: I know of no readymade way to measure fragmentation; it is easy to measure it in a disruptive way though (I think I'll write a little article on that subject). :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


      modified on Tuesday, July 14, 2009 7:19 AM

      H 1 Reply Last reply
      0
      • H himanshu2561

        Hi i have number of window services running on production server(Window server 2003 dedicated for window services).Everything was running fine from last 6 months but now some services start giving error System.OutOfMemoryException. I think this issue is due to memory leaks.Now my questions are (1)For immediate solution restarting of window services is enough or i have to restart the Production server. (2)How to find the possible cause of memory leak and suggestion for possible causes of memory leak

        himanshu

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        Your Service should be logging what method throws the Exception. You should be able to add more log messages like "I'm about to get a list of employees", etc. See if it's always the same action causing the problem (it may not be). I write a lot of Windows Services, I don't keep a lot of data in the fields, mainly just database connections. Everything else is local to the methods in use so it gets garbage collected. Review what data you keep in fields and rethink whether or not it needs to be.

        H 1 Reply Last reply
        0
        • L Luc Pattyn

          Hi, there are many possible causes. Here is one most people aren't aware of: objects larger than 80KB get allocated on the "large-object-heap", which never gets compacted. The net result is it may fragment after some time, resulting in lots of memory being free yet the heap being unable to offer a chunk of memory large enough to satisfy the latest request. Here are some candidates for large objects: - images - arrays in general - large strings (TextBox/RichTextBox often is a culprit, as it requires the concatenation of all its text) - StringBuillders and all kinds of collections since these typically are implemented as arrays as well; such objects get an initial capacity, and when insufficient, their array gets reallocated with twice the size. Solution: there isn't a perfect solution. Some suggestions: - avoid large objects as much as possible, don't use (Rich)TextBox for texts exceeding a few hundred lines - when a StringBuilder or collection will grow large, allocate it with sufficient size right away. - periodically restart your app (once a month, once a day, depending on how active its object allocation is) BTW: I know of no readymade way to measure fragmentation; it is easy to measure it in a disruptive way though (I think I'll write a little article on that subject). :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


          modified on Tuesday, July 14, 2009 7:19 AM

          H Offline
          H Offline
          himanshu2561
          wrote on last edited by
          #4

          Thanks Luc for sharing your knowledgw with us.

          Luc Pattyn wrote:

          Here are some candidates for large objects: - images - arrays in general - large strings (TextBox/RichTextBox often is a culprit, as it requires the concatenation of all its text) - StringBuillders and all kinds of collections since these typically are implemented as arrays as well; such objects get an initial capacity, and when insufficient, their array gets reallocated with twice the size.

          But possibility of above scenarios are not there in case of my window service. In my case , i am calling a web service which puts the message in the IBM MQ.Exception comes whenever i attemp to call the web service method. (1)One possible cause for exception might be that the message size is very big :confused:... waiting for your views on that (2)Also, is it possible to monitor the memory used be window service(something with performance counter or some other way) ??? (3)For immediate solution should i restart my production server or restarting of window service do the trick...

          himanshu

          L 1 Reply Last reply
          0
          • P PIEBALDconsult

            Your Service should be logging what method throws the Exception. You should be able to add more log messages like "I'm about to get a list of employees", etc. See if it's always the same action causing the problem (it may not be). I write a lot of Windows Services, I don't keep a lot of data in the fields, mainly just database connections. Everything else is local to the methods in use so it gets garbage collected. Review what data you keep in fields and rethink whether or not it needs to be.

            H Offline
            H Offline
            himanshu2561
            wrote on last edited by
            #5

            Thanks buddy..... Please look at my reply to Luc for futher clsssification of my problem.

            himanshu

            1 Reply Last reply
            0
            • H himanshu2561

              Thanks Luc for sharing your knowledgw with us.

              Luc Pattyn wrote:

              Here are some candidates for large objects: - images - arrays in general - large strings (TextBox/RichTextBox often is a culprit, as it requires the concatenation of all its text) - StringBuillders and all kinds of collections since these typically are implemented as arrays as well; such objects get an initial capacity, and when insufficient, their array gets reallocated with twice the size.

              But possibility of above scenarios are not there in case of my window service. In my case , i am calling a web service which puts the message in the IBM MQ.Exception comes whenever i attemp to call the web service method. (1)One possible cause for exception might be that the message size is very big :confused:... waiting for your views on that (2)Also, is it possible to monitor the memory used be window service(something with performance counter or some other way) ??? (3)For immediate solution should i restart my production server or restarting of window service do the trick...

              himanshu

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              Hi, (1) I have no idea what the size of those messages would be. 80KB seems rather big for a message? (2) fragmentation, the topic of my reply, is not caused by the amount of memory currently in use, it is caused by how the number and sizes of memory blocks evolve over time; and AFAIK Microsoft offers nothing to measure it. I know of an easy way to test it, but that test includes actions that fill the memory, i.e. allocate more until it fails. That probably is not what you want in an app, as it only makes sense if you can halt all threads but one, then fill the memory, then clean up again. The net result is you can prove there is fragmentation and how bad the situation is, it does not remedy anything. (3) Restarting the process that throws the OOM would technically be a solution, no matter what the exact cause of the OOM is; whether it is functionally acceptable is up to you. :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


              H 1 Reply Last reply
              0
              • L Luc Pattyn

                Hi, (1) I have no idea what the size of those messages would be. 80KB seems rather big for a message? (2) fragmentation, the topic of my reply, is not caused by the amount of memory currently in use, it is caused by how the number and sizes of memory blocks evolve over time; and AFAIK Microsoft offers nothing to measure it. I know of an easy way to test it, but that test includes actions that fill the memory, i.e. allocate more until it fails. That probably is not what you want in an app, as it only makes sense if you can halt all threads but one, then fill the memory, then clean up again. The net result is you can prove there is fragmentation and how bad the situation is, it does not remedy anything. (3) Restarting the process that throws the OOM would technically be a solution, no matter what the exact cause of the OOM is; whether it is functionally acceptable is up to you. :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                H Offline
                H Offline
                himanshu2561
                wrote on last edited by
                #7

                Thanks Luc for you reply

                Luc Pattyn wrote:

                80KB seems rather big for a message

                I also think so.

                Luc Pattyn wrote:

                fragmentation, the topic of my reply, is not caused by the amount of memory currently in use, it is caused by how the number and sizes of memory blocks evolve over time

                In that case if window service is idle for some time then why not garbage collector comes in picture and free the allocated memory

                himanshu

                L 1 Reply Last reply
                0
                • H himanshu2561

                  Thanks Luc for you reply

                  Luc Pattyn wrote:

                  80KB seems rather big for a message

                  I also think so.

                  Luc Pattyn wrote:

                  fragmentation, the topic of my reply, is not caused by the amount of memory currently in use, it is caused by how the number and sizes of memory blocks evolve over time

                  In that case if window service is idle for some time then why not garbage collector comes in picture and free the allocated memory

                  himanshu

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  I see you haven't really understood my point yet: the large-object-heap does not get compacted, i.e. when there are empty holes in the one huge memory block it manages, then the occupied blocks are NOT shifted to create one big empty hole; moving large blocks was considered too expensive. The net result is, you could have 2GB of memory filled with a pattern like this:

                  80KB in use
                  90MB free
                  80KB in use
                  90MB free
                  80KB in use
                  ...
                  80KB in use
                  90MB free
                  80KB in use
                  90MB free
                  

                  which means less than one percent is in use, and still you can't allocate a single 100MB block. That is what fragmentation is about; and that is why "memory usage" numbers don't tell you whether the situation exists or not. :)

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                  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