C # List <T> memory exception, but far from 2Gb limit
I have a List<Matrix4> , where Matrix4 is a structure containing 16 floats, so it uses 16 * 4 bytes = 64 bytes.
When I start adding items to the list, it throws an exception in memory when I cross 1 million lines.
I know that .NET has a 2Gb limit per object, but if I'm completely out of my mind:
1.000.000 * 64 bytes = ~ 61 mb
which is not even close to the limit.
When I start filling out the list, according to the task manager, my application uses 896mb, and by the time the exception is reached, it uses 1028mb.
The computer has 8 GB of physical memory, but it uses only 6 GB.
Any clues on why this might be happening?
--- UPDATE ----
Changing the target platform of the x64 platform allowed to solve the problem in a separate test project. Unfortunately, the original project cannot be x64 due to the fact that links refer to x86 DLLs that do not work on x64. But this is another problem.
I did not think about changing it to x64 because it seemed to be far from memory limitations, but I believe that the Hans Passant was right at 122mb, too close to the 1.3Gb limit. Thanks to everyone.
Large structures run in a heap of large objects (LOH) and are subject to fragmentation.
Thus, although you probably have enough free memory, you may not have enough 1 large enough block of memory.
Your numbers (1M x 64) alone are insufficient, only with a sufficient number of other distributions that will occur, this will explain the problem. You can try to solve this particular problem, but this is probably exactly the moment when a big problem becomes visible.
In general, TaskManager is not a suitable tool for diagnosing memory problems. You need a memory profiler to find out what is going on.
It also depends on your platform version and on 32 or 64 bit.