0

I'm currently studying memory management of OS by the video lecture. The instructor says,

In fact, you may have, and it is quite often the case that there may be several parts of the process memory, which are not even accessed at all. That is, they are neither executed, loaded or stored from memory.

I don't understand the saying since even if in a simple C program, we access whole address space of it. Don't we?

#include <stdio.h>
int main()
{
   printf("Hello, World!");
   return 0;
}

Could you elucidate the saying? If possible could you provide an example program wherein "several parts of the process memory, which are not even accessed at all" when it is run.

3 Answers3

1

Imagine you have a large and complicated utility (e.g. a compiler), and the user asks it for help (e.g. they type gcc --help instead of asking it to compile anything). In this case, how much of the utility's code and data is used?

Most programs have various optional parts that aren't used (e.g. maybe something that works with graphics will have some code for 16 bits per pixel and other code for 32 bits per pixel, and will determine which code to use and not use the other code). Most heap allocators are "eager" (e.g. they'll ask the OS for 20 MiB of space and then might only "malloc() 2 MiB of it). Sometimes a program will memory map a huge file but then only access a small part of it.

Even for your trivial "hello world" example code; the virtual address space probably contains a huge (several MiB) shared library to support lots of C standard library functions (e.g. puts(), fprintf(), sprintf(), ...) and your program only uses a small part of that shared library; and your program probably reserves a conservative amount of space for its stack (e.g. maybe 20 KiB of space for its stack) and then probably only uses a few hundred bytes of stack.

Brendan
  • 35,656
  • 2
  • 39
  • 66
1

In a virtual memory system, the address space of the process is created in secondary store at start up. Little or nothing gets placed in memory. For example, the operating system may use the executable file as the page file for the code and static data. It just sets up an internal structure that says some range of memory is mapped to these blocks in the executable file. The same goes for shared libraries. The other data gets mapped to the page file.

As your program runs it starts page faulting rapidly because nothing is in memory and the operating system has to load it from secondary storage.

If there is something that your program does not reference, it never gets loaded into memory.

If you had global variable declared like

char somedata [1045] ;

and your program never references that variable, it will never get loaded into memory. The same goes for code. If you have pages of code that done get execute (e.g. error handling code) it does not get loaded. If you link to shared libraries, you will likely bece including a lot of functions that you never use. Likewise, they will not get loaded if you do not execute them.

user3344003
  • 20,574
  • 3
  • 26
  • 62
0

To begin with, not all of the address space is backed by physical memory at all times, especially if your address space covers 248+ bytes, which your computer doesn't have (which is not to say you can't map most of the address space to a single physical page of memory, which would be of very little utility for anything).

And then some portions of the address space may be purposefully permanently inaccessible, like a few pages near virtual address 0 (to catch NULL pointer dereferences).

And as it's been pointed out in the other answers, with on-demand loading of programs, you may have some portions of the address space reserved for your program but if the program doesn't happen to need any of its code or data there, nothing needs to be loader there either.

Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180
  • How can an address space cover 256TiB+, isn't it too gargantuan? Is it possible? – Soner from The Ottoman Empire Mar 23 '19 at 10:40
  • @snr Of course it is possible. Just like it was 2**32 = 4GB on the 80386 when everyone had around 2MB of RAM. Look up page tables and note that their typical tree-like structure doesn't require memory to support parts of the address space that aren't mapped to physical memory. – Alexey Frunze Mar 23 '19 at 17:00