Initialize the size of array but face OutOfmemoryException

M

macneed

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(SizeArray[i++%4];
 
M

Marc Gravell

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
 
M

Marc Gravell

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
 
P

Pavel Minaev

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(SizeArray[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(SizeArray[i++%4];
and return a outofmemoryexception

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.
 
M

Marc Gravell

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.MaxGeneration, GCCollectionMode.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.
 
B

Brian Gideon

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(SizeArray[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(SizeArray[i++%4];
and return a outofmemoryexception

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top