Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Buffer is allocated from a shared heap for the process that is 64 KB in size?

Would anyone be kind enough to translate this sentence for someone blissfully ignorant of unmanaged code and intricacies of memory management?

The storage for this buffer is allocated from a shared heap for the process that is 64 KB in size. The maximum size of the buffer will depend on heap usage.

It's present on few dozen MSDN pages, e.g. WriteConsole, but I can't find any API that would calculate the maximum size such array is allowed to be before blowing up, empirically I can tell it's somewhere between 61 and 62.5 KB (by calling 64, 63, 62 etc until it stops setting DllImport's SetLastError). Is there anything like GetTotalHeapSize (if it's not a const 64KB independent of Windows version, platform architecture, defaults, etc) and something like GetCurrentHeapInUse available? How would one get the max bytes I can pass to that and other P/Invoked methods?

like image 480
Ilya Kozhevnikov Avatar asked Oct 05 '22 09:10

Ilya Kozhevnikov


1 Answers

This is not how heaps work. They don't keep track of the largest allocation you can make. It is unpredictable because a heap can get fragmented. Free blocks may be interleaved with allocated ones. Releasing a single small allocation may suddenly double the available space for example. Unmanaged heaps don't have the luxury of a managed heap, they cannot get compacted. Albeit that the .NET Large Object Heap has this problem too. There is no function to measure the largest available space, it is impossible to make that thread-safe.

Nothing you can do but try to allocate and deal with the consequences if you can't.

like image 50
Hans Passant Avatar answered Oct 10 '22 10:10

Hans Passant