What does this statement mean?

When reading the improving performance and scalability of .NET applications, I came across this in the section "Improving Managed Code Performance / Garbage Collector Tips:

Avoid preallocating and chunking memory.

But the book never explains why preallocating is bad or what Chunking really means in light of GC optimization.

Can someone shed more light on the problem and explain to me why 2 are bad and what they really mean in terms of using GC with .net?

http://www.microsoft.com/downloads/en/details.aspx?FamilyId=8A2E454D-F30E-4E72-B531-75384A0F1C47&displaylang=en

+4
source share
2 answers

Preallocated memory is a method that does not scale well. Chunking can push a selection into a bunch of large objects. Or you will make use of memory unnecessary. Trust the garbage collector, select it when you need it.

+7
source

He has a few things to say about the problems. Page 198:

C ++ programmers often allocate a large block of memory (using malloc) and then use chunks at the same time to store multiple calls to malloc. This is not recommended for managed code for several reasons:

  • Managed memory allocation is a fast operation, and the garbage collector has been optimized for extremely fast allocations. The main reason for preallocating memory in unmanaged code is to speed up the allocation process. This is not a question for managed code.
  • If you preallocate memory, you invoke more allocations than necessary; This can cause unnecessary garbage collection.
  • The garbage collector cannot recover the memory that you manually recycle.
  • Predefined memory grows and costs more processing time when it is eventually released.
+3
source

Source: https://habr.com/ru/post/1346122/


All Articles