How Does The Os Know The Real Size of The Physical Memory

how does the OS know the real size of the physical memory?

The motherboard firmware (also called BIOS, ACPI interface or EFI) allows the OS to find out the physical mapping of RAM and ROM in the system.

For example, this is the output of a booting Linux:

[    0.000000] BIOS-provided physical RAM map:
[ 0.000000] BIOS-e820: 0000000000000000 - 0000000000098c00 (usable)
[ 0.000000] BIOS-e820: 0000000000098c00 - 00000000000a0000 (reserved)
[ 0.000000] BIOS-e820: 00000000000e6000 - 0000000000100000 (reserved)
[ 0.000000] BIOS-e820: 0000000000100000 - 00000000bfea0000 (usable)
[ 0.000000] BIOS-e820: 00000000bfeae000 - 00000000bfeb0000 type 9
[ 0.000000] BIOS-e820: 00000000bfeb0000 - 00000000bfec0000 (ACPI data)
[ 0.000000] BIOS-e820: 00000000bfec0000 - 00000000bfef0000 (ACPI NVS)
[ 0.000000] BIOS-e820: 00000000bfef0000 - 00000000c0000000 (reserved)
[ 0.000000] BIOS-e820: 00000000ffc00000 - 0000000100000000 (reserved)
[ 0.000000] BIOS-e820: 0000000100000000 - 0000000c40000000 (usable)

How does Linux know how much physical memory is used by a process?

How does Linux know how much physical memory (VmRSS) is used by a process?

When the process tries to access virtual memory that has not been mapped to physical memory, the CPU (assuming it has a hardware memory management unit) will trigger an interrupt (specifically, a page fault) that is handled by the operating system. The operating system allocates the physical memory to the process and updates the translation look aside buffer (part of the MMU, which is used to map virtual memory to physical).

So, since it is the operating system that allocates the physical memory, it can also track how much it has allocated to each process.

OPERATING SYSTEMS: what is the size of the virtual memory?

If size of the physical memory is 2^32-1, then what is the size of virtual memory?

The size of the virtual address space is independent of the size of the physical address space. There is no answer.

So how much space can be allocated for a porcess in virutal memory?

That depends upon hardware limits, system parameters, and process quotas.

can this size(the space allocated for each process in the Virtual memory) exceeds the size of our RAM size?

Yes and it frequently does.

i mean if our RAM is 4GB then what is the maximum size of the virtual memory you can have for a process?

It can be anything. The rams size does not control.

can we have 4GB of virtual memory for every process or can we have more than 4GB for every process?

Both

is the Virtual memory size fixed or dynamic?

Dynamic

How much space is allocated for this memory and in the above link it is told that 2^48 is the size of virtual memory in 64 bit machine why is it only 2^48 and how can once can say a number like that?

It could be a hardware limit for a specific processor.

How can I measure the actual memory usage of an application or process?

With ps or similar tools you will only get the amount of memory pages allocated by that process. This number is correct, but:

  • does not reflect the actual amount of memory used by the application, only the amount of memory reserved for it

  • can be misleading if pages are shared, for example by several threads or by using dynamically linked libraries

If you really want to know what amount of memory your application actually uses, you need to run it within a profiler. For example, Valgrind can give you insights about the amount of memory used, and, more importantly, about possible memory leaks in your program. The heap profiler tool of Valgrind is called 'massif':

Massif is a heap profiler. It performs detailed heap profiling by taking regular snapshots of a program's heap. It produces a graph showing heap usage over time, including information about which parts of the program are responsible for the most memory allocations. The graph is supplemented by a text or HTML file that includes more information for determining where the most memory is being allocated. Massif runs programs about 20x slower than normal.

As explained in the Valgrind documentation, you need to run the program through Valgrind:

valgrind --tool=massif <executable> <arguments>

Massif writes a dump of memory usage snapshots (e.g. massif.out.12345). These provide, (1) a timeline of memory usage, (2) for each snapshot, a record of where in your program memory was allocated. A great graphical tool for analyzing these files is massif-visualizer. But I found ms_print, a simple text-based tool shipped with Valgrind, to be of great help already.

To find memory leaks, use the (default) memcheck tool of valgrind.

Why is the size of a page table entry determined only by main memory size?

Page table entries are sized to support the virtual address size and the maximum amount of physical memory supported. They are not sized based on any aspect of the secondary storage.

In your example, the page table has to support mapping 2^50 virtual pages to a possible 2^34 physical pages. Thus a page table entry will use 34 bits to hold the physical page number.

If a page is not present in memory, and it was previously paged out to secondary storage, then a data structure (like a hash table) can be used to locate where in the paging file the page is located. You don't need to use the page table structure to do this.

Why does paging let us use physical memory that is larger than what can be addressed by the CPU's address pointer length?

In the first example(4KB frame size and 232 frames), since we have
32-bit frame number and 12-bit offset, is the logical address 44-bit?

From a pure theoretical perspective that is possible but in practice a page table entry also contains information such as dirty bit, set bit and many other things for the purpose of virtual memory management.

Your book talks about it here

As we further explore paging, we introduce other information that must be kept in the page-table entries. That information reduces the number of bits available to address page frames.

So even if the page table entry size is 32-bit, we can’t use all the bits for frame numbers.
Generally in 32-bit CPU’s every process has a logical address space equal to 4 GiB and if we have frame size equal to 4096 Bytes then effectively we need 20 bits for the page numbers. Remaining 12 bits can be used to store other associated information.

In the second example(32-bit CPU using 32-bit addresses), how should I
understand the physical memory that can be addressed by the CPU's
address pointer length?

This is just extension of above stated fact. For example If you decide to use 21 bits instead of 20 for your frame numbers then you have double the logical address space to 8 Gib. However you must use some mechanism to map this 8 GiB logical address space to 4 GiB (may be swap out some pages to disk) physical address space because generally (not using some fancy technics) 32-bit CPU will be limited to 4 GiB of physical memory.

Yes

summary:- physical address space is determined by CPU's program counter. a 32-bit CPU will have a 32-bit program counter. logical address space is just an abstraction added for the purpose of security and multi programming along other things. no matter what is the size of your logical address space eventually it must be mapped to the physical address space.

watch these video's for further clarification.
Virtual Memory playlist



Related Topics



Leave a reply



Submit