Initialize the size of array but face OutOfmemoryException

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

    Initialize the size of array but face OutOfmemoryException

    i wrote a program that have a large size array, but the size is change often
    (about 3 -4 mins)

    the program like that:

    public class MemoryArray
    {
    public float[,,] MM;

    public MemoryArray(int Size)
    {
    MM = new float[Size, 10000, 10000];
    }
    }

    static void Main(string[] args)
    {
    int[] SizeArray = { 100, 200, 300, 400 };
    MemoryArray MA = new MemoryArray(1);
    int i=0;

    while (true)
    {
    MA = null; // I try to use it to release memory
    GC.Collect();

    MA = new MemoryArray(Siz eArray[i++%4];
  • Marc Gravell

    #2
    Re: Initialize the size of array but face OutOfmemoryExce ption

    Well, just a single 10k-by-10k of float is ~380MB; if you have another
    dimension too... ouch.

    so you are going to put some enormous pressure on the GC, and you'll
    probably fragment the Large Object Heap too. It might last a *bit*
    longer on Win64 with a lot of memory, but it would be better to not need
    such a big buffer.

    Dare I ask what this is for?

    Marc

    Comment

    • Pavel Minaev

      #3
      Re: Initialize the size of array but face OutOfmemoryExce ption

      On Sep 18, 3:06 pm, "macneed" <macn...@yahoo. com.hkwrote:
      i wrote a program that have a large size array, but the size is change often
      (about 3 -4 mins)
      >
      the program like that:
      >
      public class MemoryArray
      {
          public float[,,] MM;
      >
          public MemoryArray(int Size)
          {
              MM = new float[Size, 10000, 10000];
          }
      >
      }
      >
      static void Main(string[] args)
      {
          int[] SizeArray = { 100, 200, 300, 400 };
          MemoryArray MA = new MemoryArray(1);
          int i=0;
      >
          while (true)
          {
              MA = null; // I try to use it to release memory
              GC.Collect();
      >
              MA = new MemoryArray(Siz eArray[i++%4];
              .
              .
              .
      >
          }
      >
      }
      >
      But the program seems can't release memory,
      it alway break at below line after running a period of time
              MA = new MemoryArray(Siz eArray[i++%4];
      and return a outofmemoryexce ption
      My guess would be that it happens because GC.Collect() is asynchronous
      - it returns immediately, and the collection is started on a different
      thread. There's no guarantee that it completes when you next execute
      new[]. Not to mention that Collect() explicitly does not guarantee
      that all memory will be reclaimed.

      Comment

      • Marc Gravell

        #4
        Re: Initialize the size of array but face OutOfmemoryExce ption

        I just noticed that the other dimension *starts* at 100...

        100 x 10000 x 10000 x 4 = ~37GB

        That is some /serious/ memory (unless my math is wrong)

        Marc

        Comment

        • Marc Gravell

          #5
          Re: Initialize the size of array but face OutOfmemoryExce ption

          My guess would be that it happens because GC.Collect() is asynchronous
          - it returns immediately, and the collection is started on a different
          thread.
          Well, GC takes priority (i.e. suspends) your threads... but as you say,
          it isn't obliged to do *anything*. You can ask it a bit more forcefully:

          GC.Collect(GC.M axGeneration, GCCollectionMod e.Forced);

          You can also play with LatencyMode - but ultimately it is not
          recommended to mess with GC; let it do what it does best. If you are
          fighting GC, there is a good bet you are simply trying to use too much
          momory (or have a leak*).

          Marc

          *=meaning: you have left objects accidentally referenced, for example
          from a static event.

          Comment

          • Marc Gravell

            #6
            Re: Initialize the size of array but face OutOfmemoryExce ption

            See also: http://blogs.msdn.com/joshwil/archiv...10/450202.aspx

            Comment

            • Brian Gideon

              #7
              Re: Initialize the size of array but face OutOfmemoryExce ption

              On Sep 18, 6:06 am, "macneed" <macn...@yahoo. com.hkwrote:
              i wrote a program that have a large size array, but the size is change often
              (about 3 -4 mins)
              >
              the program like that:
              >
              public class MemoryArray
              {
                  public float[,,] MM;
              >
                  public MemoryArray(int Size)
                  {
                      MM = new float[Size, 10000, 10000];
                  }
              >
              }
              >
              static void Main(string[] args)
              {
                  int[] SizeArray = { 100, 200, 300, 400 };
                  MemoryArray MA = new MemoryArray(1);
                  int i=0;
              >
                  while (true)
                  {
                      MA = null; // I try to use it to release memory
                      GC.Collect();
              >
                      MA = new MemoryArray(Siz eArray[i++%4];
                      .
                      .
                      .
              >
                  }
              >
              }
              >
              But the program seems can't release memory,
              it alway break at below line after running a period of time
                      MA = new MemoryArray(Siz eArray[i++%4];
              and return a outofmemoryexce ption
              >
              how can i release a array allocated memory and reset the size of it?
              >
              THANKS
              Wow...a 100 * 10000 * 10000 array! Even if you allocated your entire
              harddrive for the page file (using a 64-bit OS would be a given) I
              still don't think that would work.

              I've got to ask...what could you possibly be doing?

              Comment

              Working...