Memory optimization for .net collections - will this method work?

Like any other large application .NET, my current project C#contains many collections .NET.
Sometimes I don’t know from the very beginning what size the collection (List / ObservableCollection / Dictionary / etc.) Will be. But there are many times when I know what it will be.

I often get OutOfMemoryException, and I was told that this can happen not only due to process size limitations, but also because of fragmentation.

So my question is this: does the collection size (using the capacity argument in the constructor) set each time I know that its expected size helps me prevent at least some of the fragmentation problems?

This quote from msdn :

If the size of the collection can be indicating the original ability eliminates the need to resize operations when adding items to the List.

But still, I don't want to start changing large parts of my code for something that might not be a real problem.

Did any of you solve your memory problems?

+5
source share
5 answers

Specifying the initial size will rarely ever get rid of the OutOfMemory problem - if your collection size is not a million objects, then you really should not store such a collection.

. , , , .

, 99 100, , .

+4

OOM, , :

, - , (, , , ). , , " ", .

, .

, . , ( ..) - , ; .

, x64 , 2 - , .

; , .

+3

.NET , , , .NET. , (, GDI +, COM ..). , , . IIRC LOH, 80kb. , , 20 000 , .

, , , : OutOfMemoryExceptions? ? , WinDbg , , LOH.

, , . , , .

+2

, .

, . , .

LOH ( 80 kB). , . , , , .

+1

, . . , , - .NET, , ?

, , , OOM. , , .

0

All Articles