Can I work from virtual memory on Linux?

My application is like a hypothetical program:

for(;;) { for (i=0; i<1000; i++) { p[i] = malloc(random_number_between_1000_and_100000()); p[i][0]=0; // update } for (i=0; i<1000; i++) { free(p[i]); } } 

It has no memory leaks, but in my system the memory consumption (at the top, the VSS column) grows without limits (for example, up to 300% of the available physical memory). This is normal?

Updated - use the memory for a while, and then free it. Is that the difference?

+4
source share
4 answers

Try to add

  sbrk(-1); 

at the end of each cycle to make sure it matters.

Free () frees memory only, but does not return its OS.

+1
source

The behavior is normal. Quoting man 3 malloc :

ERRORS

By default, Linux follows an optimistic memory allocation strategy. This means that when malloc () returns non-NULL, there is no guarantee that memory is actually available. This is a very bad mistake. In case it turns out that the system is not in memory, one or more processes will be killed by the infamous OOM killer. In the case of using Linux in conditions where it would be less desirable to suddenly lose some by accident and, in addition, the kernel version is quite modern, you can disable this excessive behavior by using the following command:

  # echo 2 > /proc/sys/vm/overcommit_memory 

See also the kernel documentation directory, vm / overcommit-accounting and sysctl / vm.txt files.

You need to touch (read / write) the memory for the Linux kernel in order to actually reserve it.

+9
source

The OS usually selects all pages as Copy-On-Write copies at "0", a page, that is, a fixed page filled with zeros. Reading from pages will return 0 as expected. While you are only reading, all links go to the same physical memory. When you write the value, "COW" will be broken and real, physical, the page frame is highlighted for you. This means that while you are not writing to memory, you can save the memory allocation until the address space of the virtual memory disappears or your page table fills all the available memory.

+1
source

Until you touch the selected pieces, the system will not actually select them for you.
However, you can run out of address space, which is the limit that the OS imposes on processes, and is not necessarily the maximum that you can handle with the type of system pointer.

0
source

All Articles