How to determine how much memory I can allocate for an array in C #

I am doing some calculations that require large array initialization. The maximum size of the array determines the maximum size of the problem that I can solve.

Is there a way to programmatically determine how much memory is available, say, the largest byte array?

thanks

+4
source share
6 answers

Well, relying on one huge array, there are a number of related problems - memory fragmentation, adjacent blocks, limitation on the maximum size of an object, etc. If you need a lot of data, I would recommend creating a class that mimics a large array using a lot of smaller (but still large) arrays, each of which has a fixed size - that is, the index is divided into the search for the corresponding array, and then uses% to get offsets inside this array.

You can also make sure you are on a 64-bit OS with lots of memory. This will give you the maximum available head room.

Depending on the scenario, more complex algorithms such as sparse arrays, eta vectors, etc., may be used to maximize what you can do. You may be surprised at what people could do many years ago with limited memory, and just a tape rotating back and forth ...

+10
source

To ensure sufficient free memory, you can use MemoryFailPoint . If memory cannot be allocated, an InsufficientMemoryException will be thrown , which you can catch and handle accordingly.

+3
source

The short answer is no. There are two top-level resources that you need to request.

  • The largest block of unallocated virtual address space available to a process
  • The amount of page file space available.

As Mark Gravell correctly stated, you will have the best success on a 64-bit platform. Here, each process has a huge virtual address space. This will effectively solve your first problem. You should also make sure the file is a large page.

But there is a better way, which is limited only by free space on your disk: files with memory mapping. You can create a large mapping (say 512 MB) into an arbitrarily large file and move it while processing your data. Please note: do not forget to open it for exclusive access.

+2
source

If you really need really large arrays, do not use the CLR. Mono supports 64-bit array indexes, allowing you to fully utilize your memory resources.

+1
source

I suppose binary search may be a way. First, start by allocating 1 byte, if it succeeds, free this byte (set the object to null) and double it to 2 bytes. Continue until you can allocate more and you have found a restriction that you can consider a "lower limit".

The correct number of bytes that can be allocated (let it be called x ) is in the range below x <2 * below . Continue searching for this interval using binary search.

0
source

The largest array that can be allocated in a 64-bit .NET program is 2 GB. (Another link.)

You can find out how many available bytes are easy enough:

Using pc As New System.Diagnostics.PerformanceCounter("Memory", "Available Bytes") FreeBytes = pc.NextValue(); End Using 

Given this information, you must make a decision.

0
source

All Articles