Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is an entire static program loaded into memory when launched?

On typical computers like Mac, Windows, Linux, iOS, etc., when a user launches a program/binary/app, is the static portion of the program always loaded entirely into memory before execution starts? Does this include all of the segments/sections of data in the program like strings and any other embedded BLOB data? Let's say I embedded a huge image file into the binary (e.g. in the __DATA segment). Would this image data be loaded entirely into memory upon launch?

like image 217
Synthetix Avatar asked Jul 30 '15 11:07

Synthetix


People also ask

What loads into memory when process is launched?

For a program to execute as a process, it has to be loaded into memory via a program loader. The executable file that contains the program consists of several sections that identify different regions of the program and where they should be loaded into memory.

When a program is run it is loaded into memory?

When a program is loaded into the memory it is called as process.

What is only loaded into memory if they are required?

A process must be loaded into memory in order to execute. If there is not enough memory available to keep all running processes in memory at the same time, then some processes who are not currently using the CPU may have their memory swapped out to a fast local disk called the backing store.

What is loaded into the RAM?

When you run a program, the executable itself and its associated resources like images, dlls and other modules needed by executable are loaded into RAM. For example when you open an image from an executable, both executable and image are loaded into RAM.


1 Answers

Under OS X, Windows, Linux, and iOS executables are not loaded into RAM when executed. Instead the executable is mapped into the virtual address space of the process. When the process accesses a mapped page of the executable that hasn't loaded into RAM yet, the CPU generates a page fault which the OS handles by reading the page into RAM.

So if you put a huge image file in the data section of your executable, it won't be loaded into RAM until your program first accesses it. A huge image file probably takes of multiple pages of memory (which are generally 4K in size), so if your program only accesses part of the image only part of the image will be loaded into RAM.

Note that under Windows, and maybe other operating systems, there's an significant exception to this. Under Windows an operating system service called the prefetcher will start preloading into memory the parts of any file that it predicts the program will access during start up. It makes these predictions based on the recorded start up access patterns of previous runs of the program. Since "any file" includes the executable itself, along with any DLLs or data files it uses, this means parts of the executable will be preloaded into RAM when the process start. This also means that if the program usually displays a large image at program startup (eg. a splash screen) then the preloader will load the image into RAM whether its stored as part of the executable or as a separate data file.

like image 161
Ross Ridge Avatar answered Oct 24 '22 05:10

Ross Ridge