Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why so many applications allocate incredibly large amount of virtual memory while not using any of it?

I've been watching some weird phenomena in programming for quite some time, since overcommit is enabled by default on linux systems.

It seems to me that pretty much every high level application (eg. application written in high level programming language like Java, Python or C# including some desktop applications written in C++ that use large libraries such as Qt) use insane amount of virtual operating memory. For example, it's normal for web browser to allocate 20GB of ram while using only 300MB of it. Or for a dektop environment, mysql server, pretty much every java or mono application and so on, to allocate tens of gigabytes of RAM.

Why is that happening? What is the point? Is there any benefit in this?

I noticed that when I disable overcommit in linux, in case of a desktop system that actually runs a lot of these applications, the system becomes unusable as it doesn't even boot up properly.

like image 460
Petr Avatar asked Sep 26 '22 08:09

Petr


People also ask

Why do I have more virtual memory than physical memory?

Virtual memory allows the programmer to use more memory for the programs than the available physical memory. If the physical memory is 4GB and the virtual memory is 16GB, the programmer can use the 16GB virtual memory to execute the program.

What happens if you allocate too much virtual memory?

The bigger the virtual memory space, the bigger the adress table becomes in which is written, which virtual adress belongs to which physical adress. A big table can theoreticaly result in slower translation of the adresses and therefore in slower reading and writing speeds.

Why is virtual memory so important?

Virtual memory frees up RAM by swapping data that has not been used recently over to a storage device, such as a hard drive or solid-state drive (SSD). Virtual memory is important for improving system performance, multitasking and using large programs.

What is the main problem of using virtual memory?

One potential problem with virtual memory is that if the amount of RAM present is too small compared to the amount of virtual memory then a system can end up spending a large proportion of its CPU resources swapping data back and forth.


2 Answers

Languages that run their code inside virtual machines (like Java (*), C# or Python) usually assign large amounts of (virtual) memory right at startup. Part of this is necessary for the virtual machine itself, part is pre-allocated to parcel out to the application inside the VM.

With languages executing under direct OS control (like C or C++), this is not necessary. You can write applications that dynamically use just the amount of memory they actually require. However, some applications / frameworks are still designed in such a way that they request a large chunk memory from the operating system once, and then manage the memory themselves, in hopes of being more efficient about it than the OS.

There are problems with this:

  • It is not necessarily faster. Most operating systems are already quite smart about how they manage their memory. Rule #1 of optimization, measure, optimize, measure.

  • Not all operating systems do have virtual memory. There are some quite capable ones out there that cannot run applications that are so "careless" in assuming that you can allocate lots & lots of "not real" memory without problems.

  • You already found out that if you turn your OS from "generous" to "strict", these memory hogs fall flat on their noses. ;-)


(*) Java, for example, cannot expand its VM once it is started. You have to give the maximum size of the VM as a parameter (-Xmxn). Thinking "better safe than sorry" leads to severe overallocations by certain people / applications.

like image 99
DevSolar Avatar answered Oct 20 '22 09:10

DevSolar


These applications usually have their own method of memory management, which is optimized for their own usage and is more efficient than the default memory management provided by the system. So they allocate huge memory block, to skip or minimize the effect of the memory management provided by system or libc.

like image 1
Zang MingJie Avatar answered Oct 20 '22 08:10

Zang MingJie