memory leak or not?

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

    memory leak or not?

    Does such a function result in a memory leak because of not explicitely
    setting result = null?
    ------------------
    public override string GetValueAsStrin g()
    {
    StringBuilder result = new StringBuilder(1 0);
    result.Append(M onth.AsString() );
    result.Append('/');
    result.Append(D ay.AsString());
    result.Append('/');
    result.Append(Y ear.AsString()) ;
    return result.ToString ();
    }
    ------------------

    Or does the compiler automatically gets that "result" goes off focus now?

    best regards,
    doc


  • Marina Levit [MVP]

    #2
    Re: memory leak or not?

    No, when the variable is out of scope, it becomes eligible for garbage
    collection.

    "docschnipp " <docschnipp@new sgroup.nospamwr ote in message
    news:D52F0CD9-91F8-4238-A100-19BAC61089BA@mi crosoft.com...
    Does such a function result in a memory leak because of not explicitely
    setting result = null?
    ------------------
    public override string GetValueAsStrin g()
    {
    StringBuilder result = new StringBuilder(1 0);
    result.Append(M onth.AsString() );
    result.Append('/');
    result.Append(D ay.AsString());
    result.Append('/');
    result.Append(Y ear.AsString()) ;
    return result.ToString ();
    }
    ------------------
    >
    Or does the compiler automatically gets that "result" goes off focus now?
    >
    best regards,
    doc
    >
    >

    Comment

    • docschnipp

      #3
      Re: memory leak or not?

      Hi Marina,

      "Marina Levit [MVP]" wrote:
      No, when the variable is out of scope, it becomes eligible for garbage
      collection.
      >
      Thanks for confirmation. Moving from C++ where one has to take care of
      everything makes me paranoid sometimes :)

      doc

      Comment

      • Martin Z

        #4
        Re: memory leak or not?


        docschnipp wrote:
        Hi Marina,
        >
        "Marina Levit [MVP]" wrote:
        >
        No, when the variable is out of scope, it becomes eligible for garbage
        collection.
        >
        Thanks for confirmation. Moving from C++ where one has to take care of
        everything makes me paranoid sometimes :)
        >
        doc
        In that case, a warning: unless you use the "Using" block, you can't do
        the neat tricks in C++ where destructors made objects clean up their
        resources the moment they go out of focus. So on the one hand, GC
        means memory leaking is much less likely, it also means that your
        objects may retain resources (like file and DB connections) a lot
        longer unless you manually free them - objects falling off the stack
        don't clean up their resources until the GC runs, which may be a little
        while.

        Comment

        • Jeremy Cox

          #5
          Re: memory leak or not?

          You omit to mention that this is for automatic calling of the Dispose()
          method and not a dtor as such

          To Marina - check whether the object in question implements IDisposable. If
          so you should scope it within a using {} block. Dispose() will then get
          automatically called when this block is left.


          "Martin Z" <martin.zarate@ gmail.comwrote in message
          news:1162403842 .467831.317260@ e64g2000cwd.goo glegroups.com.. .
          >
          docschnipp wrote:
          >Hi Marina,
          >>
          >"Marina Levit [MVP]" wrote:
          >>
          No, when the variable is out of scope, it becomes eligible for garbage
          collection.
          >
          >>
          >Thanks for confirmation. Moving from C++ where one has to take care of
          >everything makes me paranoid sometimes :)
          >>
          >doc
          >
          In that case, a warning: unless you use the "Using" block, you can't do
          the neat tricks in C++ where destructors made objects clean up their
          resources the moment they go out of focus. So on the one hand, GC
          means memory leaking is much less likely, it also means that your
          objects may retain resources (like file and DB connections) a lot
          longer unless you manually free them - objects falling off the stack
          don't clean up their resources until the GC runs, which may be a little
          while.
          >

          Comment

          • Marina Levit [MVP]

            #6
            Re: memory leak or not?

            Sorry, why is the comment to me?

            All I said was once it is out of scope, it becomes eligible to be garbage
            collected. This is true regardless of whetehr or not it implements
            IDisposable.

            "Jeremy Cox" <jeremy.cox@tes co.netwrote in message
            news:IR52h.6986 9$pa.46612@news fe2-gui.ntli.net...
            You omit to mention that this is for automatic calling of the Dispose()
            method and not a dtor as such
            >
            To Marina - check whether the object in question implements IDisposable.
            If so you should scope it within a using {} block. Dispose() will then get
            automatically called when this block is left.
            >
            >
            "Martin Z" <martin.zarate@ gmail.comwrote in message
            news:1162403842 .467831.317260@ e64g2000cwd.goo glegroups.com.. .
            >>
            >docschnipp wrote:
            >>Hi Marina,
            >>>
            >>"Marina Levit [MVP]" wrote:
            >>>
            >No, when the variable is out of scope, it becomes eligible for garbage
            >collection.
            >>
            >>>
            >>Thanks for confirmation. Moving from C++ where one has to take care of
            >>everything makes me paranoid sometimes :)
            >>>
            >>doc
            >>
            >In that case, a warning: unless you use the "Using" block, you can't do
            >the neat tricks in C++ where destructors made objects clean up their
            >resources the moment they go out of focus. So on the one hand, GC
            >means memory leaking is much less likely, it also means that your
            >objects may retain resources (like file and DB connections) a lot
            >longer unless you manually free them - objects falling off the stack
            >don't clean up their resources until the GC runs, which may be a little
            >while.
            >>
            >
            >

            Comment

            • docschnipp

              #7
              Re: memory leak or not?



              "Martin Z" wrote:
              >
              docschnipp wrote:
              Thanks for confirmation. Moving from C++ where one has to take care of
              everything makes me paranoid sometimes :)

              doc
              >
              In that case, a warning: unless you use the "Using" block, you can't do
              the neat tricks in C++ where destructors made objects clean up their
              resources the moment they go out of focus. So on the one hand, GC
              means memory leaking is much less likely, it also means that your
              objects may retain resources (like file and DB connections) a lot
              longer unless you manually free them - objects falling off the stack
              don't clean up their resources until the GC runs, which may be a little
              while.
              Yes, I am aware of that fact. I started to do 2 things:

              1) build my own "release" functions to gain control of other resources than
              memory
              2) despite the "going off scope" I am partially using an extra "objref =
              null" to move the object on top of the GC. Especially when reusing a ref.

              Doc

              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: memory leak or not?

                Marina Levit [MVP] <someone@nospam .comwrote:
                No, when the variable is out of scope, it becomes eligible for garbage
                collection.
                In this particular case it's true, but in a more general case it can be
                earlier. For instance:

                void Foo()
                {
                string x = GrabSomeLongStr ing();
                Console.WriteLi ne (x.Length);

                // Lots more code here

                // Nothing referring to x

                // End of method
                }

                The variable "x" does not prevent the long string from being garbage
                collected (in release mode) before the end of the method, even though
                it's still in scope.

                --
                Jon Skeet - <skeet@pobox.co m>
                http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                If replying to the group, please do not mail me too

                Comment

                • Kevin Yu [MSFT]

                  #9
                  Re: memory leak or not?

                  Yes, I agree with Marina. After the using statement, it only calls the
                  Dispose method to release the unmanaged resources. In the case doc has
                  provided, everything is managed. So, all are the same. When the reference
                  to StringBuilder goes out of scope, it becomes garbage collectable.

                  There is no memory leak in this case.

                  Kevin Yu
                  Microsoft Online Community Support

                  =============== =============== =============== =====
                  Get notification to my posts through email? Please refer to
                  Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.

                  ications.
                  Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
                  where an initial response from the community or a Microsoft Support
                  Engineer within 1 business day is acceptable. Please note that each follow
                  up response may take approximately 2 business days as the support
                  professional working with you may need further investigation to reach the
                  most efficient resolution. The offering is not appropriate for situations
                  that require urgent, real-time or phone-based interactions or complex
                  project analysis and dump analysis issues. Issues of this nature are best
                  handled working with a dedicated Microsoft Support Engineer by contacting
                  Microsoft Customer Support Services (CSS) at
                  http://msdn.microsoft.com/subscripti...t/default.aspx.
                  =============== =============== =============== =====

                  (This posting is provided "AS IS", with no warranties, and confers no
                  rights.)

                  Comment

                  • Cor Ligthert [MVP]

                    #10
                    Re: memory leak or not?

                    Jeremy,

                    It seems that a lot of people, like in my idea you, are thinking that
                    dispose is the same as the classic finalize method. I hope I don't disapoint
                    you: It is not. Marina's answer is completely correct whether Idispose is
                    implemented or not. Only if there are unmanaged resources used, then
                    Idisposable comes in scope.

                    Cor

                    "Jeremy Cox" <jeremy.cox@tes co.netschreef in bericht
                    news:IR52h.6986 9$pa.46612@news fe2-gui.ntli.net...
                    You omit to mention that this is for automatic calling of the Dispose()
                    method and not a dtor as such
                    >
                    To Marina - check whether the object in question implements IDisposable.
                    If so you should scope it within a using {} block. Dispose() will then get
                    automatically called when this block is left.
                    >
                    >
                    "Martin Z" <martin.zarate@ gmail.comwrote in message
                    news:1162403842 .467831.317260@ e64g2000cwd.goo glegroups.com.. .
                    >>
                    >docschnipp wrote:
                    >>Hi Marina,
                    >>>
                    >>"Marina Levit [MVP]" wrote:
                    >>>
                    >No, when the variable is out of scope, it becomes eligible for garbage
                    >collection.
                    >>
                    >>>
                    >>Thanks for confirmation. Moving from C++ where one has to take care of
                    >>everything makes me paranoid sometimes :)
                    >>>
                    >>doc
                    >>
                    >In that case, a warning: unless you use the "Using" block, you can't do
                    >the neat tricks in C++ where destructors made objects clean up their
                    >resources the moment they go out of focus. So on the one hand, GC
                    >means memory leaking is much less likely, it also means that your
                    >objects may retain resources (like file and DB connections) a lot
                    >longer unless you manually free them - objects falling off the stack
                    >don't clean up their resources until the GC runs, which may be a little
                    >while.
                    >>
                    >
                    >

                    Comment

                    • Cor Ligthert [MVP]

                      #11
                      Re: memory leak or not?

                      Jon,

                      I am glad I can help you with this.

                      The string is immutable what you are showing is not a reference.

                      You can have a look at what immuatble means at these pages.



                      :-)

                      Cor

                      "Jon Skeet [C# MVP]" <skeet@pobox.co mschreef in bericht
                      news:MPG.1fb32d aa702cb9a898d5b 1@msnews.micros oft.com...
                      Marina Levit [MVP] <someone@nospam .comwrote:
                      >No, when the variable is out of scope, it becomes eligible for garbage
                      >collection.
                      >
                      In this particular case it's true, but in a more general case it can be
                      earlier. For instance:
                      >
                      void Foo()
                      {
                      string x = GrabSomeLongStr ing();
                      Console.WriteLi ne (x.Length);
                      >
                      // Lots more code here
                      >
                      // Nothing referring to x
                      >
                      // End of method
                      }
                      >
                      The variable "x" does not prevent the long string from being garbage
                      collected (in release mode) before the end of the method, even though
                      it's still in scope.
                      >
                      --
                      Jon Skeet - <skeet@pobox.co m>
                      http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                      If replying to the group, please do not mail me too

                      Comment

                      • Cor Ligthert [MVP]

                        #12
                        Re: memory leak or not?

                        "Reusing a ref" (in other words declaring it globaly) what is the sense of
                        that?



                        "docschnipp " <docschnipp@new sgroup.nospamsc hreef in bericht
                        news:DA678407-B3E6-47C8-AF0E-BA059FCBA697@mi crosoft.com...
                        >
                        >
                        "Martin Z" wrote:
                        >
                        >>
                        >docschnipp wrote:
                        Thanks for confirmation. Moving from C++ where one has to take care of
                        everything makes me paranoid sometimes :)
                        >
                        doc
                        >>
                        >In that case, a warning: unless you use the "Using" block, you can't do
                        >the neat tricks in C++ where destructors made objects clean up their
                        >resources the moment they go out of focus. So on the one hand, GC
                        >means memory leaking is much less likely, it also means that your
                        >objects may retain resources (like file and DB connections) a lot
                        >longer unless you manually free them - objects falling off the stack
                        >don't clean up their resources until the GC runs, which may be a little
                        >while.
                        >
                        Yes, I am aware of that fact. I started to do 2 things:
                        >
                        1) build my own "release" functions to gain control of other resources
                        than
                        memory
                        2) despite the "going off scope" I am partially using an extra "objref =
                        null" to move the object on top of the GC. Especially when reusing a ref.
                        >
                        Doc
                        >

                        Comment

                        • docschnipp

                          #13
                          Re: memory leak or not?

                          "Cor Ligthert [MVP]" wrote:
                          "Reusing a ref" (in other words declaring it globaly) what is the sense of
                          that?
                          In this case it is a member of my class, like a reference to an object or a
                          collection.

                          I was asking myself if such a construct would help the compiler/runtime to
                          reuse the same memory block:

                          this.mysomethin g = null;
                          this.mysomethin g = new Something();

                          or if the null assignment is just waste of source code lines :)

                          I guess the latter.

                          cu
                          doc

                          Comment

                          • Willy Denoyette [MVP]

                            #14
                            Re: memory leak or not?


                            "Cor Ligthert [MVP]" <notmyfirstname @planet.nlwrote in message
                            news:euFV1Jk$GH A.4592@TK2MSFTN GP03.phx.gbl...
                            | Jon,
                            |
                            | I am glad I can help you with this.
                            |
                            | The string is immutable what you are showing is not a reference.
                            |
                            | You can have a look at what immuatble means at these pages.
                            |
                            | http://www.yoda.arachsys.com/csharp/parameters.html
                            |
                            | :-)



                            Not sure what you are talking about, what Jon is trying to say is that an
                            object can be GC'd even before it's reference leaves the scope.

                            Willy.


                            Comment

                            • Göran Andersson

                              #15
                              Re: memory leak or not?

                              docschnipp wrote:
                              "Cor Ligthert [MVP]" wrote:
                              >
                              >"Reusing a ref" (in other words declaring it globaly) what is the sense of
                              >that?
                              >
                              In this case it is a member of my class, like a reference to an object or a
                              collection.
                              >
                              I was asking myself if such a construct would help the compiler/runtime to
                              reuse the same memory block:
                              >
                              this.mysomethin g = null;
                              this.mysomethin g = new Something();
                              >
                              or if the null assignment is just waste of source code lines :)
                              >
                              I guess the latter.
                              >
                              cu
                              doc
                              >
                              If you expect the allocation to almost always cause a garbage collection
                              because it in turn creates an obscene amount of objects, then it makes
                              sense to dereference the previous object first. Otherwise it's just a
                              waste of code and time.

                              Comment

                              Working...