Memory usage. ( H E L P ! ! )

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • George Gre

    #1

    Memory usage. ( H E L P ! ! )

    Hi,

    I wrote a c# programme that listens to incoming TCP requests and services
    them. This programme is meant to be running as long as the server its
    installed on is running. So we assume for ever(!). My problem is that I have
    it running for 2 days now on my pc (win2Ksp3 P4 2GHz,512MB Ram - framework
    v1.0.3705) and the taskmanager reports that this process takes 97MB ram/130
    VMemory and it looks that its eating up more ram(!!).

    To help out the garbage collector I tried to use as much stacked based
    variables. Also for the objects created if they had a dispose() method I am
    calling it when I am done with the object. If the objects dont have a
    dispose() method, I am setting the refference to null. I am using ADODB to
    get a recordset from a VB DLL. What is the best way to tell the GC that I am
    done with the object, since the ADODB.RecordCla ss does not have a dispose
    method?

    Do they suffice?? Do you have links to relevant articles??

    Any help will be much appreciated,
    -George.


  • Jon Skeet

    #2
    Re: Memory usage. ( H E L P ! ! )

    George Gre <Giorgos@totali se.co.uk> wrote:[color=blue]
    > I am not using GC.Collect(), because of some MSDN articles suggesting to
    > leave the GC alone to figure things out about the mem usage on the app. If
    > you want I can send you these articles.[/color]

    No, I quite agree that for the most part you should let the GC get on
    with it. However, when diagnosing problems, you should consider calling
    GC.Collect() periodically and seeing whether or not it reduces the
    memory. If it does, then the effect you're seeing is just the GC not
    being terribly aggressive. If it doesn't, you've probably got a leak
    somewhere.
    [color=blue]
    > My app works on async calls on the TCP and I dont have a main thread. Should
    > I call gc.collect() every time I finish servicing a tcp message?[/color]

    Certainly not every message. If the GC is being much too timid, you
    could *consider* calling it every half hour, say - or possibly if the
    total memory is above a certain level.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • George Gre

      #3
      Re: Memory usage. ( H E L P ! ! )

      OK. I will try it and see what happens.

      I got another question:
      When using COM+ dll was is the best option of activation new() or
      server.CreateOb ject()?
      When done with the object just nulling the refference will gc know that the
      object is to be collected? The object does not have a Dispose() method..

      George
      "Jon Skeet" <skeet@pobox.co m> wrote in message
      news:MPG.1975ea 0919837b698a088 @news.microsoft .com...[color=blue]
      > George Gre <Giorgos@totali se.co.uk> wrote:[color=green]
      > > I am not using GC.Collect(), because of some MSDN articles suggesting to
      > > leave the GC alone to figure things out about the mem usage on the app.[/color][/color]
      If[color=blue][color=green]
      > > you want I can send you these articles.[/color]
      >
      > No, I quite agree that for the most part you should let the GC get on
      > with it. However, when diagnosing problems, you should consider calling
      > GC.Collect() periodically and seeing whether or not it reduces the
      > memory. If it does, then the effect you're seeing is just the GC not
      > being terribly aggressive. If it doesn't, you've probably got a leak
      > somewhere.
      >[color=green]
      > > My app works on async calls on the TCP and I dont have a main thread.[/color][/color]
      Should[color=blue][color=green]
      > > I call gc.collect() every time I finish servicing a tcp message?[/color]
      >
      > Certainly not every message. If the GC is being much too timid, you
      > could *consider* calling it every half hour, say - or possibly if the
      > total memory is above a certain level.
      >
      > --
      > Jon Skeet - <skeet@pobox.co m>
      > http://www.pobox.com/~skeet/
      > If replying to the group, please do not mail me too[/color]


      Comment

      • George Gre

        #4
        Re: Memory usage. ( H E L P ! ! )

        I have added the GC.Collect() (for testing only) after I am done servicing
        tcp request, as Jon suggested .

        I am running my service for 1 hour now and the mem usage now varies between
        14.3MB to 15MB and VM 7.5MB and stays there!!!

        So it looks that I dont have a memory leak, its just that the gc was not
        proporly collecting the objects.
        Maybe because I am usinginterop, since I call a COM+ vb dll during servicing
        the tcp requests. Or maybe because I use very small byte arrays (<50bytes)
        and the gc never decides to free them.

        It looks that the framework has a problem. Now I am thinking that I need to
        upgrade to framework 1.1 (I got 1.0.3705).

        Jon many thanks for your suggestion.

        George.

        "Jon Skeet" <skeet@pobox.co m> wrote in message
        news:MPG.1975ff 2c85a38d298a08b @news.microsoft .com...[color=blue]
        > George Gre <Giorgos@totali se.co.uk> wrote:[color=green]
        > > OK. I will try it and see what happens.
        > >
        > > I got another question:
        > > When using COM+ dll was is the best option of activation new() or
        > > server.CreateOb ject()?[/color]
        >
        > That I don't know much about, I'm afraid - I try to avoid COM wherever
        > possible!
        >[color=green]
        > > When done with the object just nulling the refference will gc know that[/color][/color]
        the[color=blue][color=green]
        > > object is to be collected? The object does not have a Dispose() method..[/color]
        >
        > You don't need to null the reference variable if it's either a local
        > variable which is going to fall out of scope or not be used for the
        > rest of the method, or if it's an instance variable but the whole
        > object itself is going to become eligible for collection soon.
        >
        > --
        > Jon Skeet - <skeet@pobox.co m>
        > http://www.pobox.com/~skeet/
        > If replying to the group, please do not mail me too[/color]


        Comment

        • Chris Torgerson

          #5
          Re: Memory usage. ( H E L P ! ! )

          Also, take a look at The ReleaseComObjec t call on the
          system.runtime. interopservices .marshal class. It should address some of
          your COM disposal.

          chris torgerson

          "George Gre" <Giorgos@totali se.co.uk> wrote in message
          news:uK$zkQgRDH A.2480@tk2msftn gp13.phx.gbl...[color=blue]
          > OK. I will try it and see what happens.
          >
          > I got another question:
          > When using COM+ dll was is the best option of activation new() or
          > server.CreateOb ject()?
          > When done with the object just nulling the refference will gc know that[/color]
          the[color=blue]
          > object is to be collected? The object does not have a Dispose() method..
          >
          > George
          > "Jon Skeet" <skeet@pobox.co m> wrote in message
          > news:MPG.1975ea 0919837b698a088 @news.microsoft .com...[color=green]
          > > George Gre <Giorgos@totali se.co.uk> wrote:[color=darkred]
          > > > I am not using GC.Collect(), because of some MSDN articles suggesting[/color][/color][/color]
          to[color=blue][color=green][color=darkred]
          > > > leave the GC alone to figure things out about the mem usage on the[/color][/color][/color]
          app.[color=blue]
          > If[color=green][color=darkred]
          > > > you want I can send you these articles.[/color]
          > >
          > > No, I quite agree that for the most part you should let the GC get on
          > > with it. However, when diagnosing problems, you should consider calling
          > > GC.Collect() periodically and seeing whether or not it reduces the
          > > memory. If it does, then the effect you're seeing is just the GC not
          > > being terribly aggressive. If it doesn't, you've probably got a leak
          > > somewhere.
          > >[color=darkred]
          > > > My app works on async calls on the TCP and I dont have a main thread.[/color][/color]
          > Should[color=green][color=darkred]
          > > > I call gc.collect() every time I finish servicing a tcp message?[/color]
          > >
          > > Certainly not every message. If the GC is being much too timid, you
          > > could *consider* calling it every half hour, say - or possibly if the
          > > total memory is above a certain level.
          > >
          > > --
          > > Jon Skeet - <skeet@pobox.co m>
          > > http://www.pobox.com/~skeet/
          > > If replying to the group, please do not mail me too[/color]
          >
          >[/color]


          Comment

          • Mark Pearce

            #6
            Re: Memory usage. ( H E L P ! ! )

            Hi George,

            When you measure memory usage of your process, are you measuring the memory
            allocated or the memory actually used? If you're measuring the memory
            allocated, it's not a problem as that memory will be re-allocated if other
            apps need it.

            I would be very wary of calling GC.Collect on a regular basis in a
            production app. The GC has a good understanding of the local environment,
            and usually a much better understanding of GC-related memory and performance
            issues than the developer.

            Regards,

            Mark
            --
            Author of "Comprehens ive VB .NET Debugging"



            "George Gre" <Giorgos@totali se.co.uk> wrote in message
            news:uQ9zkshRDH A.3796@tk2msftn gp13.phx.gbl...
            I have added the GC.Collect() (for testing only) after I am done servicing
            tcp request, as Jon suggested .

            I am running my service for 1 hour now and the mem usage now varies between
            14.3MB to 15MB and VM 7.5MB and stays there!!!

            So it looks that I dont have a memory leak, its just that the gc was not
            proporly collecting the objects.
            Maybe because I am usinginterop, since I call a COM+ vb dll during servicing
            the tcp requests. Or maybe because I use very small byte arrays (<50bytes)
            and the gc never decides to free them.

            It looks that the framework has a problem. Now I am thinking that I need to
            upgrade to framework 1.1 (I got 1.0.3705).

            Jon many thanks for your suggestion.

            George.

            "Jon Skeet" <skeet@pobox.co m> wrote in message
            news:MPG.1975ff 2c85a38d298a08b @news.microsoft .com...[color=blue]
            > George Gre <Giorgos@totali se.co.uk> wrote:[color=green]
            > > OK. I will try it and see what happens.
            > >
            > > I got another question:
            > > When using COM+ dll was is the best option of activation new() or
            > > server.CreateOb ject()?[/color]
            >
            > That I don't know much about, I'm afraid - I try to avoid COM wherever
            > possible!
            >[color=green]
            > > When done with the object just nulling the refference will gc know that[/color][/color]
            the[color=blue][color=green]
            > > object is to be collected? The object does not have a Dispose() method..[/color]
            >
            > You don't need to null the reference variable if it's either a local
            > variable which is going to fall out of scope or not be used for the
            > rest of the method, or if it's an instance variable but the whole
            > object itself is going to become eligible for collection soon.
            >
            > --
            > Jon Skeet - <skeet@pobox.co m>
            > http://www.pobox.com/~skeet/
            > If replying to the group, please do not mail me too[/color]



            Comment

            • George Gre

              #7
              Re: Memory usage. ( H E L P ! ! )

              For anyone still interested:

              I have apllied the SP2 for the .NET framework and re-run my test. I left my
              service running for almost 4 days(!) and it looks
              like that the SP2 did the trick. The service does not look to have any
              memory leaks and the service is behaving fine!

              -George.


              "Nostromo" <bogus@bogus.co m> wrote in message
              news:OEknGVrRDH A.3144@tk2msftn gp13.phx.gbl...[color=blue]
              > The GC:Collect() was only used to prove that my code does not leak memory.
              > Also using the profiler from systech.com I could
              > see that equal number of objects created where destroyed, but task manager
              > still reported that the apps memory usage was always going
              > upwards. Also the DLLHOST.exe that was hosting my COM+ app was leaking
              > memory (mem usage was going upwards..).I suppose the gc was not releasing
              > the
              > references on the COM+ objects.
              >
              > After using GC.Collect and ReleasCOMRef, I was able to prove that atleast[/color]
              my[color=blue]
              > code was not behaving that bad!!!
              >
              > Today I will apply the SP on the framework and rerun my testing with out[/color]
              the[color=blue]
              > GC.Collect() and ReleaseCOMRef, and see what happens.
              >
              > - George
              >
              > "Mark Pearce" <evil@bay.com > wrote in message
              > news:e6CEAxnRDH A.3768@tk2msftn gp13.phx.gbl...[color=green]
              > > Hi George,
              > >
              > > When you measure memory usage of your process, are you measuring the[/color]
              > memory[color=green]
              > > allocated or the memory actually used? If you're measuring the memory
              > > allocated, it's not a problem as that memory will be re-allocated if[/color][/color]
              other[color=blue][color=green]
              > > apps need it.
              > >
              > > I would be very wary of calling GC.Collect on a regular basis in a
              > > production app. The GC has a good understanding of the local[/color][/color]
              environment,[color=blue][color=green]
              > > and usually a much better understanding of GC-related memory and[/color]
              > performance[color=green]
              > > issues than the developer.
              > >
              > > Regards,
              > >
              > > Mark
              > > --
              > > Author of "Comprehens ive VB .NET Debugging"
              > > http://www.apress.com/book/bookDisplay.html?bID=128
              > >
              > >
              > > "George Gre" <Giorgos@totali se.co.uk> wrote in message
              > > news:uQ9zkshRDH A.3796@tk2msftn gp13.phx.gbl...
              > > I have added the GC.Collect() (for testing only) after I am done[/color][/color]
              servicing[color=blue][color=green]
              > > tcp request, as Jon suggested .
              > >
              > > I am running my service for 1 hour now and the mem usage now varies[/color]
              > between[color=green]
              > > 14.3MB to 15MB and VM 7.5MB and stays there!!!
              > >
              > > So it looks that I dont have a memory leak, its just that the gc was not
              > > proporly collecting the objects.
              > > Maybe because I am usinginterop, since I call a COM+ vb dll during[/color]
              > servicing[color=green]
              > > the tcp requests. Or maybe because I use very small byte arrays[/color][/color]
              (<50bytes)[color=blue][color=green]
              > > and the gc never decides to free them.
              > >
              > > It looks that the framework has a problem. Now I am thinking that I need[/color]
              > to[color=green]
              > > upgrade to framework 1.1 (I got 1.0.3705).
              > >
              > > Jon many thanks for your suggestion.
              > >
              > > George.
              > >
              > > "Jon Skeet" <skeet@pobox.co m> wrote in message
              > > news:MPG.1975ff 2c85a38d298a08b @news.microsoft .com...[color=darkred]
              > > > George Gre <Giorgos@totali se.co.uk> wrote:
              > > > > OK. I will try it and see what happens.
              > > > >
              > > > > I got another question:
              > > > > When using COM+ dll was is the best option of activation new() or
              > > > > server.CreateOb ject()?
              > > >
              > > > That I don't know much about, I'm afraid - I try to avoid COM wherever
              > > > possible!
              > > >
              > > > > When done with the object just nulling the refference will gc know[/color][/color]
              > that[color=green]
              > > the[color=darkred]
              > > > > object is to be collected? The object does not have a Dispose()[/color][/color]
              > method..[color=green][color=darkred]
              > > >
              > > > You don't need to null the reference variable if it's either a local
              > > > variable which is going to fall out of scope or not be used for the
              > > > rest of the method, or if it's an instance variable but the whole
              > > > object itself is going to become eligible for collection soon.
              > > >
              > > > --
              > > > Jon Skeet - <skeet@pobox.co m>
              > > > http://www.pobox.com/~skeet/
              > > > If replying to the group, please do not mail me too[/color]
              > >
              > >
              > >[/color]
              >
              >[/color]


              Comment

              • Chris Hornberger

                #8
                Re: Memory usage. ( H E L P ! ! )

                If you're using a very tight loop in your program and have to idle
                time, GC may *never* run or may not run until the system absolutely
                deems it 100% necessary. Too many people do something like:

                for(;;){
                CheckForConnect ions();
                }

                and wonder why things are having problems. Is something like this in
                effect in your program?

                "George Gre" <Giorgos@totali se.co.uk> wrote in message news:<uCXUroeRD HA.2460@TK2MSFT NGP10.phx.gbl>. ..[color=blue]
                > Hi,
                >
                > I wrote a c# programme that listens to incoming TCP requests and services
                > them. This programme is meant to be running as long as the server its
                > installed on is running. So we assume for ever(!). My problem is that I have
                > it running for 2 days now on my pc (win2Ksp3 P4 2GHz,512MB Ram - framework
                > v1.0.3705) and the taskmanager reports that this process takes 97MB ram/130
                > VMemory and it looks that its eating up more ram(!!).
                >
                > To help out the garbage collector I tried to use as much stacked based
                > variables. Also for the objects created if they had a dispose() method I am
                > calling it when I am done with the object. If the objects dont have a
                > dispose() method, I am setting the refference to null. I am using ADODB to
                > get a recordset from a VB DLL. What is the best way to tell the GC that I am
                > done with the object, since the ADODB.RecordCla ss does not have a dispose
                > method?
                >
                > Do they suffice?? Do you have links to relevant articles??
                >
                > Any help will be much appreciated,
                > -George.[/color]

                Comment

                Working...