Reputation: 1595
A major application of mine, is crashing at run time.
I want to find out if this is related to a memory allocation issue by the system. Thus, I created a small test program to allocate 1GB of memory and simultaneously ran 15 such processes, thus using 15GB of RAM in total.
However, when I run this program the task manager shows that it has occupied only 2GB of RAM? How is that possible?
I wrote a sample code as follows
char *ptr[1024];
for ( i = 0 ; i < 1024 ; ++i )
{
ptr[i] = new char[1024 * 1024];
std::cout << " Allocated 1024 MB" << i << " th time " << std::endl;
}
Upvotes: 4
Views: 811
Reputation: 941705
Windows is a demand-paged virtual memory operating system. Allocating memory with operator new only allocates virtual memory. You won't start using physical memory, RAM, until you actually access the memory. Which you did not do.
You can force the RAM to be allocated by touching every 4096th byte, better make that a write or it will be optimized away:
size_t size = 1024 * 1024;
ptr[i] = new char[size];
for (size_t j = 0; j < size; j += 4096) ptr[i][j] = 0;
But that's quite a pointless thing to do, it just slows your program down. And doesn't actually test anything, a process cannot run out of RAM on Windows. Put Task Manager into programmer mode, add the column for Commit size. That's the real number.
Upvotes: 13
Reputation: 129374
The OS (whether Windows, Linux or MacOS) doesn't expect that all processes that allocate memory to actually USE all that memory. Lots of programs do things like allocate 1MB to hold a file, and then fill it with 14KB of file-data (because it's easier than figuring out how large the file is and actually allocate the right amount). So, instead of going through all the effort of actually making a real physical memory page available for memory that may not be used, it just leaves it allocated in virtual space, and then finds some physical memory when the memory is actually used.
Thus the amount of memory shown in Task Manager is only the actual memory you are using, not the memory you have allocated but never used.
If you write a loop that writes to every 256th or 1024th byte in your allocation, it should "grow" and show your actual amount of allocated memory.
Upvotes: 1
Reputation: 569
http://en.wikipedia.org/wiki/Virtual_memory
You are essentially filling your pagefile with data. The OS sees that you are allocating lots of data, but until you use it, this data won't be pulled from the page file into main memory.
Upvotes: 0
Reputation:
Maybe the OS overcommits, i. e., operator new
returns a valid (non-NULL
) pointer although there's not enough memory. Try actually writing something to the memory obtained, and your process will probably be killed (or at least the physical memory usage goes up).
Upvotes: 1
Reputation: 9434
Try storing data in your big arrays. Memset would do fine. You are probably looking at actual memory if you don't touch it these could be still only in virtual memroy.
Upvotes: 8