Im trying to allocate preciesly 1 KiB of memory by the usage of pointers
GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc));
std::cout << pmc.WorkingSetSize << " Current physical memory used by the process" << std::endl;
int a = pmc.WorkingSetSize;
char *test= new char[1024];
GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc));
int b = pmc.WorkingSetSize;
std::cout << "Actual allocated " << (b - a) / 1024 << std::endl;
problem is everytime i run this code it seems to allocate anywhere between 100 KiB to 400 KiB i used char since it's 1 byte in size
The implementation is free to allocate more than you asked for behind your back in order to be able to satisfy future (expected) allocations faster (without having to involve the OS). This is usually a good optimization and nothing to worry about - especially since operating systems may not even back that allcation with actual physical pages before you actually write to them, so it often costs nothing at all.
In any case, from a C++ language perspective; as long as you got the memory you requested, whether or not the implementation actually allocated more is not usually something you can know nor something you should care about.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With