简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM