Less memory usage when running similar software?

2

I have a question I often wonder about. Do programs use less RAM memory, if a similar program is already running?

What do I mean by similar? Well for example I wonder about any Chromium derivate and Atom running at the same time. Isn't Atom a Chormium browser under the hood? So if they use the same libraries at some point, does it save me memory, compared to running some other hypothetical editor or browser with on its own same memory consumption as Atom or Chromium?

Another example: Firefox and Thunderbird - Any saving there?

Shouldn't the OS be so clever to recognize similar dependencies and then save memory?

Zelphir Kaltstahl

Posted 2016-08-01T15:44:30.230

Reputation: 194

1

You may want to read http://unix.stackexchange.com/a/116332/167583 regarding shared libraries. Note that Chromium also uses shared memory which can possibly alleviate its global memory usage.

– Julie Pelletier – 2016-08-01T16:02:12.453

@JuliePelletier Pretty complex stuff, but I think I got the gist of it: Depending on how a shared library is compiled, it can be used by two programs without loading it twice. (?) – Zelphir Kaltstahl – 2016-08-01T16:17:11.030

That is indeed the gist of it. – Julie Pelletier – 2016-08-01T16:18:30.157

Answers

1

Yes, they use less memory. Actually, your example of Chromium vs. Atom are a perfect illustration of this. Both browsers use the WebKit library to render web pages. If both browsers are using the same version of the WebKit library, the operating system only loads one copy of it into memory for both of them to use. This process is called dynamic linking, hence the name Dynamic Link Library (DLL) on Windows. Linux just calls them 'libraries'.

Almost every program you run farms a lot of its functionality out to an external library of some kind. When the program invokes one of the functions in a library, the operating system loads a copy of the library into memory, and then maps the function's address into the program's memory space.

Say program X calls a function in library A. The OS loads library A into memory and gives program X a pointer to the function it needs. Now let's say that program Y also calls the same function in library A. Since library A is already in memory (because program X is using it), the OS does not load it again. It simply passes another pointer to program Y.

It's important to note that both program X and program Y have their own references to the same function in library A, and neither is aware that it's being used by the other. They both think that the function is mapped into their own memory space. The OS is dynamically translating the virtualized pointer addresses to their actual values on behalf of each program. The OS is essentially lying to each of them about what memory address corresponds to the function.

Now, let's say that program X modifies some bytes of memory held by that function. Because the memory is shared between two different programs, the OS makes a second copy of the function so that program Y remains unaffected by the change. At this point, the function is now loaded into memory twice. Neither program is made aware of this, however. Program X and Y just continue doing their thing and the OS handles which one gets what copy. This technique is called copy-on-write.

In this way, many programs can all be working on the same copy of a given piece of memory without having multiple copies of it in RAM. This virtualization of memory is one of the reasons why it's so hard to get a proper accounting of how much RAM is actually in use in a system.

Many software vendors have their own in-house function libraries they use, so even if two programs are not similar at all (like Firefox and Thunderbird), the fact that they are from the same vendor (Mozilla in this case) increases the likelihood that they will be able to use shared memory for their functions.

Wes Sayeed

Posted 2016-08-01T15:44:30.230

Reputation: 12 024