Memory allocation - how can 15 GB be equal to 2 GB?

The main use of mine, rumbling at runtime.

I want to find out if this is due to the issue of system memory allocation. Thus, I created a small test program to allocate 1 GB of memory and at the same time performed 15 such processes, using a total of 15 GB of RAM.

However, when I run this program, does the task manager show that it took up only 2 GB of RAM? How is this possible?

I wrote an example 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; } 
+4
source share
5 answers

Try storing data in large arrays. Memset will be fine. You probably look at real memory, if you don’t touch it, it can only be in virtual memory.

+8
source

Windows is an on-demand virtual memory operating system. Allocation of memory by the new operator assigns virtual memory. You will not start using physical memory, RAM, until you gain access to the memory. What you did not do.

You can force the allocation of RAM by touching each 4096th byte, it is better to record or it will be optimized:

  size_t size = 1024 * 1024; ptr[i] = new char[size]; for (size_t j = 0; j < size; j += 4096) ptr[i][j] = 0; 

But this is a completely pointless thing, it just slows down your program. And in fact, it does not test anything, the process cannot end from RAM to Windows. Put the task manager in programmer mode, add a column for the size of Commit. This is a real number.

+14
source

Maybe OS overcommits, ie, operator new returns a valid (not NULL ) pointer, although there is not enough memory. Try to actually write something to the resulting memory, and your process is likely to be killed (or at least the use of physical memory is increasing).

+1
source

The OS (whether Windows, Linux, or MacOS) does not expect all processes that allocate memory to actually use all this memory. Many programs perform functions such as allocating 1 MB to store a file, and then fill it with 14 KB of file data (because it’s easier than figuring out how big the file is and actually allocating the right amount). Thus, instead of performing all efforts to create real physical memory accessible to memory that cannot be used, it simply leaves it in virtual space and then finds some physical memory when the memory is actually used.

Thus, the amount of memory displayed in the task manager is only the actual memory that you are using, not the memory that you allocated but did not use.

If you write a loop that writes every 256th or 1024th byte in your allocation, it should "grow" and show your actual amount of allocated memory.

+1
source

http://en.wikipedia.org/wiki/Virtual_memory

You essentially populate your page file with data. The OS sees that you allocate a lot of data, but until you use it, this data will not be extracted from the page file into main memory.

0
source

Source: https://habr.com/ru/post/1498666/


All Articles