启动时是否将整个静态程序加载到内存中? [英] Is an entire static program loaded into memory when launched?

查看:173
本文介绍了启动时是否将整个静态程序加载到内存中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Mac,Windows,Linux,iOS等典型计算机上,当用户启动程序/二进制文件/应用程序时,程序的静态部分是否总是在执行开始之前始终全部加载到内存中?这是否包括程序中所有数据段/节,例如字符串和任何其他嵌入式BLOB数据?假设我在二进制文件中嵌入了一个巨大的图像文件(例如,在__DATA段中).启动后,此图像数据会完全加载到内存中吗?

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?

推荐答案

在OS X下,执行时不会将Windows可执行文件,Windows,Linux和iOS可执行文件加载到RAM中.而是将可执行文件映射到进程的虚拟地址空间.当进程访问尚未加载到RAM中的可执行文件的映射页面时,CPU会生成页面错误,操作系统会通过将页面读入RAM来处理该页面错误.

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.

因此,如果将巨大的图像文件放在可执行文件的数据部分中,则在程序首次访问它之前,该文件不会被加载到RAM中.巨大的图像文件可能会占用多页内存(通常大小为4K),因此,如果您的程序仅访问部分图像,则仅部分图像会被加载到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.

请注意,在Windows以及其他操作系统下,有一个明显的例外.在Windows下,称为prefetcher的操作系统服务将开始将它预测程序在启动过程中将访问的任何文件的部分预加载到内存中.它基于程序先前运行的记录的启动访问模式进行这些预测.由于任何文件"包括可执行文件本身,以及它使用的任何DLL或数据文件,因此这意味着可执行文件的一部分将在进程启动时预加载到RAM中.这也意味着,如果程序通常在程序启动时显示大图像(例如,启动画面),则预加载器会将图像加载到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.

这篇关于启动时是否将整个静态程序加载到内存中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆