.NET stack memory limit

I am using C #, .NET 4.0, 64-bit. I need to store in memory 500 million "data points" that are used in the calculations. I need to decide whether to create them as objects of a structure or class. Structures seem a lot faster.

Is there a memory limit for the stack? If so, how can it be configured.

Will it store so much data on the stack, affecting the overall system performance?

(By the way, I know about the limitation of the size of one object in .NET, therefore, to be addressed, the data will be stored in several collections).

+6
source share
3 answers

You are asking the wrong question. If the stack size matters, you are doing something wrong.

If you use many datapoints, you will put them in a collection such as an array. Arrays are always allocated per heap. An array of structures embeds individual structures and forms a continuous block of memory. (If you have more than 2 GB, you need several arrays).

While with reference types, the array will contain only links, and objects are obtained separately on the heap. The heap distribution has about 16 bytes of overhead, the link in the array takes into account another 8.
You will also get worse cache locality due to limitations, and the GC needs to do more work to circumvent all of these links.

My conclusion is that if you have a lot of small data, make it a structure and put it in an array.

+6
source

You are going to store your data in arrays, and arrays are always stored on the heap. Therefore, it doesn’t matter if you use structures or classes to store these arrays. You may want to make sure that your data points are value types (i.e. Structures) so that data point arrays can be efficiently allocated in adjacent blocks of memory.

Differences in performance between the distributed heap and stack memory are more likely to be visible with small objects that are allocated and freed up in a short amount of time. For long-lived objects of the size you describe, I would expect that there will be no performance difference between the stack and the memory allocated by the heap.

+4
source

You can use classes for your data points. In this case, the memory will be allocated on the heap.

But given that you are talking about 500 million data points, and especially since you are programming in the .NET world with a more limited memory limit for applications, I highly recommend using a built-in database like sqlite, for example. This way you avoid using all of your data points in memory at the same time, but only the ones you need to calculate.

+1
source

Source: https://habr.com/ru/post/910979/


All Articles