Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# List<T> out of memory exception but far away from the 2Gb limit

I have a List<Matrix4>, where Matrix4 is a struct containing 16 floats, so it uses 16 * 4 bytes = 64 bytes.

When I start adding items to the list it throws an Out Of Memory Exception when I cross the 1 million line.

I know that .NET have a limit of 2Gb per object, but unless I'm completely out of my mind:

1.000.000 * 64 bytes = ~61mb

Which is not even close to the limit.

When I start populating the list, according to task manager, my application is using 896mb and by the time I reach the exception it's using 1028mb.

The computer has 8GB of physical memory but it's using only 6Gb.

Any clues on why is could be happening?

--- UPDATE ----

Changing the platform target to x64 solved the issue on a separate test project. Unfortunately the original project cannot be x64 due references do x86 DLLs that do not work on x64. But that's another problem.

I didn't thought on changing it to x64 because it seemed to be far from the memory limits, but I guess Hans Passant was right on 122mb being too close from the 1.3Gb limit. Thank you all.

like image 928
rbasniak Avatar asked Jun 30 '15 19:06

rbasniak


People also ask

What is C in simple words?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is %d in C programming?

In C programming language, %d and %i are format specifiers as where %d specifies the type of variable as decimal and %i specifies the type as integer. In usage terms, there is no difference in printf() function output while printing a number using %d or %i but using scanf the difference occurs.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


1 Answers

Large structures are done on the Large Object Heap (LOH) and that is subject to fragmentation.

So while you probably have enough free memory you may not have 1 large enough block of memory left.

Your numbers (1M x 64) are not enough by them self, only with enough other allocations going on it would explain the issue. You could try to solve this particular issue but it's probably just the point where a larger problem becomes visible.

In general, TaskManager is not the right tool to diagnose memory problems. You need a memory profiler to find out what's going on.

It also depends on the version of your platform and on whether it is 32 or 64 bits.

like image 118
Henk Holterman Avatar answered Sep 18 '22 20:09

Henk Holterman