Does a static program run into memory on startup?

On regular computers like Mac, Windows, Linux, iOS, etc., when a user launches a program / binary / application, is the static part of the program always fully loaded into memory before launch? Does this include all data segments / sections in the program like strings and any other inline BLOB data? Let's say I have embedded a huge image file in a binary (for example in the __DATA segment). Will this image data be loaded completely into memory at startup?

+3


source to share


1 answer


On OS X, Windows, Linux, and iOS binaries are not loaded into RAM at runtime. Instead, the executable is mapped to the virtual address space of the process. When a process accesses the displayed page of an executable file that has not yet been loaded into RAM, the CPU generates a page error, which the OS handles when 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 receives it. The huge image file is likely to take up several pages of memory (which are usually 4KB in size), so if your program only accesses a portion of the image, only a portion of the image will be loaded into RAM.



Note that there is a significant exception on Windows and possibly other operating systems. On Windows, an operating system service called a prefetcher will begin to preload into memory portions of whatever file it predicts the program will access during startup. It makes these predictions based on the recorded access patterns to start previous runs of the program. Since "any file" includes the executable itself, as well as any DLLs or data files it uses, this means that portions of the executable will be preloaded into RAM when the process starts. This also means that if the program normally displays a large image when the program starts (like a splash screen), then the preloader loads the image into RAM, regardless of whetherwhether it is stored as part of the executable file or as a separate data file.

+8


source







All Articles