Re: Memory allocation problem with c++/cli
"Bob Milton" wrote:
Oh, but reserved memory does cost. Just try starting
2000 suspended threads and see how long your program
lasts.
No, reserved memory is not backed up by physical memory, so
its cost is negligible. Starting 2000 threads will require
to reserve 2GB of address space. It's impossible because 2GB
of address space is all that process has for user mode code.
2000 threads just won't fit in a process. Moreover, starting
thread requires allocation of thread bookkeeping info in
kernel: thread context, handle, etc.. So, new thread
requires more resources than reserving 1MB for its stack.
If you don't believe me, then run this program:
int _tmain(int argc, _TCHAR* argv[])
{
// Reserve 1.5GB
LPVOID pMem = VirtualAlloc(NULL, 0x60000000,
MEM_RESERVE, PAGE_READWRITE);
_gettchar();
VirtualFree(pMem, 0, MEM_RELEASE);
return 0;
}
It reserves 1.5GB of memory. Before you press <Enter> look
at the process in Task Manager. No slightest thing will
change.