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

时间:2015-06-30 19:21:09

标签: c# .net out-of-memory

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.

1 个答案:

答案 0 :(得分:11)

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.