473,398 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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(SizeArray[i++%4];
Sep 18 '08 #1
6 1545
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
Sep 18 '08 #2
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(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.
Sep 18 '08 #3
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
Sep 18 '08 #4
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.
Sep 18 '08 #5
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(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?
Sep 18 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Mark Hannon | last post by:
I am trying to initialize an array only once so it can be seen & used by any functions that need it. As I understand it, if a variable is declared by itself outside of any functions, its scope is...
2
by: slack_justyb | last post by:
Hello, I'm trying to figure the best way of doing the following. *I need an array of strings that are globally accessable from within 'arrayhere.h' *I need that array of strings to not change...
3
by: jut_bit_zx | last post by:
class A { public: A(); virtual ~A(){} .... private: int m_iarray; }
7
by: arkobose | last post by:
hey everyone! i have this little problem. consider the following declaration: char *array = {"wilson", "string of any size", "etc", "input"}; this is a common data structure used to store...
15
by: Geoff Cox | last post by:
Hello, Can I separately declare and initialize a string array? How and where would I do it in the code below? It was created using Visual C++ 2005 Express Beta 2 ... In C# I would have ...
6
by: Scirious | last post by:
People, how can I know how big an object is? I mean, I have an object the collects data from a stream and when it grows to an especific size I need to create a new object to continue collecting the...
18
by: toton | last post by:
Hi, In C++ when I initialize an array it, also initializes the class that it contains, which calls the default constructor. However, I want to initialize the array only (i.e reserve the space) and...
2
by: heng | last post by:
If the data member of a class is an array, how to initialize? I tried the following, but it is wrong. class A { public: int a; A():a({0,0,0}){} };
1
by: macneed | last post by:
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.