When a program causes a .so file to be loaded into memory, does is get loaded as a whole, or or does it only load the necessary "chunks"?


No. First the .so file is opened, then mmap() creates the virtual address space necessary to hold the whole file contents. However, only when something tries to access a particular address in the space will the access cause a page fault, and the kernel will read a (4k) block from the file corresponding to the page's offset into real memory. When it is in actual memory, the access will be restarted. This is demand paging.

| improve this answer | |
  • '... the kernel will read PAGE_SIZE block...' PAGE_SIZE depends on architecture – Serge Jun 1 '16 at 21:33
  • @Serge Sure, but all popular architectures use 4kB (and many obscure ones too). – Gilles 'SO- stop being evil' Jun 1 '16 at 22:05
  • 1
    @Gilles I commented for the sake of completeness, nothing else. – Serge Jun 1 '16 at 22:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.