Releasing memory held by array?

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

    Releasing memory held by array?

    How can I manually release the memory held by a large array? I have
    tried setting the array to null but no joy. The garbage collector
    appears to be ignoring it completely.

  • Jon Skeet [C# MVP]

    #2
    Re: Releasing memory held by array?

    Chris Ashley wrote:
    How can I manually release the memory held by a large array? I have
    tried setting the array to null but no joy. The garbage collector
    appears to be ignoring it completely.
    How have you come to the conclusion that it's being ignored? Can you
    give a short but complete example demonstrating the problem?

    Jon

    Comment

    • Chris  Ashley

      #3
      Re: Releasing memory held by array?


      Jon Skeet [C# MVP] wrote:
      How have you come to the conclusion that it's being ignored? Can you
      give a short but complete example demonstrating the problem?
      >
      Jon
      public void SetByteData()
      {
      int intTotalStrips = (infoHdr.biHeig ht / STRIP_HEIGHT) + 1;
      int intLastStripHei ght = infoHdr.biHeigh t % STRIP_HEIGHT;
      theBitmaps = new RawBitmap[intTotalStrips];

      // Generate standard strips
      for (int i = 0; i < intTotalStrips; i++)
      {
      theBitmaps[i] = new RawBitmap(fileH dr, infoHdr);
      theBitmaps[i].SetHeight(STRI P_HEIGHT);
      theBitmaps[i].BytImgData = ReadLines(ioBin aryStream,
      STRIP_HEIGHT);
      }
      // Generate last strip
      int intLastBound = theBitmaps.GetU pperBound(0);
      //theBitmaps[intLastBound] = new RawBitmap(fileH dr, infoHdr);
      //theBitmaps[intLastBound].SetHeight(intL astStripHeight) ;
      //theBitmaps[intLastBound].BytImgData =
      ReadLines(ioBin aryStream, intLastStripHei ght);

      }


      This is where my memory shoots up. The RawBitmap class holds a huge
      array of bytes, and in turn the class that this method belongs to holds
      approximately 100 instances of RawBitmap in an array. The method is
      being called from an ASPX page but the memory is never being released,
      even at the end of the page lifecycle. In task manager the aspnet
      process increases its memory usage with each refresh of the page.

      Comment

      • Chris  Ashley

        #4
        Re: Releasing memory held by array?


        Chris Ashley wrote:
        This is where my memory shoots up. The RawBitmap class holds a huge
        array of bytes, and in turn the class that this method belongs to holds
        approximately 100 instances of RawBitmap in an array. The method is
        being called from an ASPX page but the memory is never being released,
        even at the end of the page lifecycle. In task manager the aspnet
        process increases its memory usage with each refresh of the page.
        Manually calling GC.Collect in the Dispose method for my class appears
        to have solved it.

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: Releasing memory held by array?

          Chris Ashley wrote:
          Chris Ashley wrote:
          This is where my memory shoots up. The RawBitmap class holds a huge
          array of bytes, and in turn the class that this method belongs to holds
          approximately 100 instances of RawBitmap in an array. The method is
          being called from an ASPX page but the memory is never being released,
          even at the end of the page lifecycle. In task manager the aspnet
          process increases its memory usage with each refresh of the page.
          >
          Manually calling GC.Collect in the Dispose method for my class appears
          to have solved it.
          That suggests it's reached generation 2 - it would eventually get
          collected, but there's nothing to trigger garbage collection
          automatically at the end of the page lifecycle.

          I *think* you'd have found that after a fair number of requests, the GC
          would have collected the array.

          Jon

          Comment

          • Nicholas Paldino [.NET/C# MVP]

            #6
            Re: Releasing memory held by array?

            It doesn't necessarily have to go to Gen 2. If the arrays that the
            RawBitmap instances point to are large enough (the OP says that it holds a
            huge array of bytes), then those might go to the Large Object Heap.

            As for calling GC.Collect, it's not the best in ASP.NET environments.
            ASP.NET apps get into a more predictable routine for GC than say, windows
            apps (which have more erratic spikes when it comes to allocating and
            deallocating memory). Calling GC.Collect will disrupt that routine.

            And for what? You (the OP) are using Task Manager to gauge whether or
            not memory is being released. Task Manager only reports the ^working set^
            to you, and not whether or not your memory is being collected properly.

            If you are going to performance tune an ASP.NET application, you need to
            know what to look at. In this case, it is the performance counters in .NET,
            not Task Manager. If anything, you ^should^ see the value in Task Manager
            increase. I'd almost say that if it wasn't as you do work, there was
            something wrong.

            Here is a link to a list of performance counters which would be of help
            to you:



            And by all means, take out the call to GC.Collect in your Dispose
            implementation. It's not helping you. In fact, you are probably going to
            see a drop in throughput as a result.

            --
            - Nicholas Paldino [.NET/C# MVP]
            - mvp@spam.guard. caspershouse.co m

            "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
            news:1157553056 .259859.251610@ b28g2000cwb.goo glegroups.com.. .
            Chris Ashley wrote:
            >Chris Ashley wrote:
            This is where my memory shoots up. The RawBitmap class holds a huge
            array of bytes, and in turn the class that this method belongs to holds
            approximately 100 instances of RawBitmap in an array. The method is
            being called from an ASPX page but the memory is never being released,
            even at the end of the page lifecycle. In task manager the aspnet
            process increases its memory usage with each refresh of the page.
            >>
            >Manually calling GC.Collect in the Dispose method for my class appears
            >to have solved it.
            >
            That suggests it's reached generation 2 - it would eventually get
            collected, but there's nothing to trigger garbage collection
            automatically at the end of the page lifecycle.
            >
            I *think* you'd have found that after a fair number of requests, the GC
            would have collected the array.
            >
            Jon
            >

            Comment

            • Nicholas Paldino [.NET/C# MVP]

              #7
              Re: Releasing memory held by array?

              And to elaborate even more, one of the people at MS just wrote about why
              you shouldn't use Task Manager this morning:




              --
              - Nicholas Paldino [.NET/C# MVP]
              - mvp@spam.guard. caspershouse.co m

              "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c omwrote in
              message news:ewEqPZc0GH A.4976@TK2MSFTN GP02.phx.gbl...
              It doesn't necessarily have to go to Gen 2. If the arrays that the
              RawBitmap instances point to are large enough (the OP says that it holds a
              huge array of bytes), then those might go to the Large Object Heap.
              >
              As for calling GC.Collect, it's not the best in ASP.NET environments.
              ASP.NET apps get into a more predictable routine for GC than say, windows
              apps (which have more erratic spikes when it comes to allocating and
              deallocating memory). Calling GC.Collect will disrupt that routine.
              >
              And for what? You (the OP) are using Task Manager to gauge whether or
              not memory is being released. Task Manager only reports the ^working set^
              to you, and not whether or not your memory is being collected properly.
              >
              If you are going to performance tune an ASP.NET application, you need
              to know what to look at. In this case, it is the performance counters in
              .NET, not Task Manager. If anything, you ^should^ see the value in Task
              Manager increase. I'd almost say that if it wasn't as you do work, there
              was something wrong.
              >
              Here is a link to a list of performance counters which would be of help
              to you:
              >

              >
              And by all means, take out the call to GC.Collect in your Dispose
              implementation. It's not helping you. In fact, you are probably going to
              see a drop in throughput as a result.
              >
              --
              - Nicholas Paldino [.NET/C# MVP]
              - mvp@spam.guard. caspershouse.co m
              >
              "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
              news:1157553056 .259859.251610@ b28g2000cwb.goo glegroups.com.. .
              >Chris Ashley wrote:
              >>Chris Ashley wrote:
              >This is where my memory shoots up. The RawBitmap class holds a huge
              >array of bytes, and in turn the class that this method belongs to
              >holds
              >approximatel y 100 instances of RawBitmap in an array. The method is
              >being called from an ASPX page but the memory is never being released,
              >even at the end of the page lifecycle. In task manager the aspnet
              >process increases its memory usage with each refresh of the page.
              >>>
              >>Manually calling GC.Collect in the Dispose method for my class appears
              >>to have solved it.
              >>
              >That suggests it's reached generation 2 - it would eventually get
              >collected, but there's nothing to trigger garbage collection
              >automaticall y at the end of the page lifecycle.
              >>
              >I *think* you'd have found that after a fair number of requests, the GC
              >would have collected the array.
              >>
              >Jon
              >>
              >
              >

              Comment

              Working...