Reputation: 617
I created a simple program that allocates a integer array onto the heap/free store every time the user clicks 'enter'. My ulitmate goal was to see a change in the heap memory size with each new allocation. I do this by checking the status file to see the change in heap memory size. Unfortunately, at each allocation, the status file doesn't update. It seems like the only way I could get the file to update is if I spam the program with a bunch of carriage returns and then it updates. Why does it do this? I need a reliable method to determine the range of memory my programs are taking up from dynamic allocations as I am working on an embedded system. Can anyone provide any insights to /proc//status or another method one could determine heap memory size?
Also status provides the memory in KB, it would be nice to see this information with more granularity to the byte - is there a way to do this? Thank you.
Here is a sample program of what I did:
#include <cstddef>
#include <iostream>
int keepLooping()
{
return 1;
}
int main (int argc, char* argv[])
{
int exit_code = 0;
int fd = -1;
do
{
do
{
std::cout << '\n' << "Press a key to allocate data to the heap...";
} while (std::cin.get() != '\n');
int *someArray = new int[1000];
}
while (keepLooping());
return exit_code;
}
Upvotes: 0
Views: 301
Reputation: 238421
C++ implementations don't typically allocate every dynamic allocation directly from the operating system, but rather request entire pages of memory (4kB by default on Linux) or more and distribute smaller allocations within the process. Therefore it is unlikely to be able for the operating system to observe smaller changes in the memory use of the process.
On some systems, Linux in particular given certain configuration, allocating dynamic storage doesn't necessarily use any system resources. Such systems which over commit the memory do not see any change in the use of physical memory until the allocated memory is actually accessed.
There are memory profiling tools such as valgrind which can be used to wrap the process and accurately measure the use of heap memory throughout the execution of the process.
Upvotes: 2