Memory

From DikapediaV2
Revision as of 00:47, 27 August 2024 by Ardika Sulistija (talk | contribs) (Created page with "===What is Memory?=== ---- What is <b>memory</b>? * Just a form of storage. * Computers have many layers of memory, the higher up you go, the slower to access: ** CPU registers (takes 1 CPU cycle) ** CPU cache, (L1, L2, L3) (takes 3 - 14 CPU cycles) ** RAM (~250 CPU cycles) ** Disk (~40 million) <b>Physical Memory</b> vs <b>Virtual memory</b> * Physical memory (aka main memory) memory is provided as a map to the OS. * This map is divided into page frames of 4KB each...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

What is Memory?



What is memory?

  • Just a form of storage.
  • Computers have many layers of memory, the higher up you go, the slower to access:
    • CPU registers (takes 1 CPU cycle)
    • CPU cache, (L1, L2, L3) (takes 3 - 14 CPU cycles)
    • RAM (~250 CPU cycles)
    • Disk (~40 million)


Physical Memory vs Virtual memory

  • Physical memory (aka main memory) memory is provided as a map to the OS.
  • This map is divided into page frames of 4KB each.
  • Each GB of memory is ~250K pages.
  • Main memory is never allocated directly to a process. Processes get virtual memory which is mapped back to physical/main memory.
  • When a process gets their allocation of virtual memory, they don’t think that they are other processes.
  • They think they are the only thing on the system and only look at what they have. This sort of provides the isolation between processes.
  • The virtual memory that the processes use is mapped back to real memory via page tables.
    • The page tables just contain information such as the virtual memory address and the map back to the main memory address.


[Add image]


Paging


Paging is the movement of pages (the 4KB) into and out of the main memory.

Benefits:

  • Allows partially loaded programs to execute
  • Allows programs larger than main memory to execute. This is how you can run 40GB games and you only have say 16GB of RAM. It's possible because of paging.
  • Efficient movement of programs between main memory and storage
  • Fine grained, unlike swapping - swapping moves entire programs in/out, paging only moves pages (4KB).

There are two types of pages:

  • File System Paging
    • Reading/writing of pages in memory mapped files (mmap()) and on file systems that use page cache. (“Good paging”)
  • Kernel can free memory by paging
  • Once a file system page has been modified in main memory, it is considered a “dirty page”, and requires write to disk.
  • “Clean page” or not modified, the page out just frees the memory immediately.
  • “Page out” means the page has been moved out of physical memory, keep in mind this operation could have also included writing the page to a storage device before being moved out.
  • Anonymous Paging
    • These are private to processes - process heaps(?) and stacks(?).
    • Its called anonymous due to lack of a named location in the OS. and is also known as “Bad paging” because it hurts performance.
    • Anonymous page outs require the data be moved to physical swap devices or swap files, ‘swapping.’
    • Applications requiring access to anonymous pages that have been PAGED OUT require anonymous PAGE IN, which is a blocking I/O call to the disk.


  • Page out; when pages are written to disk. Or when the paged has been moved out of physical memory.
  • Page in; when pages are returned to memory. (Read from disk)
    • Page outs themselves may not negatively affect performance as they can be done asynchronously, while page ins are synchronous.
  • On Demand Paging-Page Faults
    • On demand paging is the act of mapping pages of virtual memory to main memory on demand. It defers CPU overhead of creating mapping until they are needed and accessed, instead of when memory is first allocated.

A page fault occurs when a page that is allocated but has no virtual memory <==> physical memory page mapping, is accessed.

  • Despite the word "fault" a page fault is normal operation. It is how the kernel achieves on demand paging.
  • If the mapping can be satisfied from another page in memory, it is called a minor fault which may occur for mapping a new page from available memory. It can also occur when mapping to an existing page, such as reading a page from a shared library.


Page Cache


  • The Page Cache:

https://www.oreilly.com/library/view/understanding-the-linux/0596005652/ch15s01.html

  • Storing Blocks in the Page Cache:

https://www.oreilly.com/library/view/understanding-the-linux/0596005652/ch15s02.html

  • Wikipedia: Page Cache:

https://en.wikipedia.org/wiki/Page_cache

  • Linux Page Cache Basics:

https://www.thomas-krenn.com/en/wiki/Linux_Page_Cache_Basics


In the Linux kernel, it uses cached memory for disk caching which is also known as the page cache. This "disk cache" or page cache stores whole pages of disk data resulting from accessing the contents of the disk files. When reading from a disk or writing to a disk, the kernel will refer to the page cache. For example, if a process sends out a read request, a new "page" is added to the page cache if the page is not already in the cache, which would contain the data read from the disk. If there is enough free memory, the page is kept in the cache and can then be reused by other processes without accessing the disk directly. This makes the system faster and more responsive as it allows quicker access to the contents that were read from the disk via the page cache. For instance, this is why typically when you open a file a second time, it tends to be quicker than the first time.



Out of Memory Troubleshooting



Once Linux loads a file into RAM, it doesn’t necessarily remove it from RAM when a program is done with it. If there is RAM available, Linux will cache the file in RAM so that if a program needs to access the file again, it can do so much more quickly. If the system does need RAM for active processes, it won’t cache as many files.


Swap Space


Swap Space - The portion of virtual memory that is on the hard disk used when RAM is full.

  • It is a space on the hard disk which is substitute of physical memory.
  • It is used as virtual memory which contains process memory image. Whenever the system runs short of physical memory it uses its virtual memory and stores in memory on disk.
  • Swap space helps the computer's operating system in pretending that is has more RAM than it actually does.
  • AKA swap file
  • This interchange of data between virtual memory and real/physical memory is called swapping and the space on disk is swap space.


Swap space can be useful to computer in various ways:

  • It can be used as a single contiguous memory which reduces i/o operations to read or write a file.
  • Applications which are not used or are used less can be kept in swap file.
  • Having sufficient swap files helps the system keep some physical memory free all the time.
  • The space in physical memory which has been freed due to swap space can be used by OS for some other important tasks.


Out-of-Memory (OOM) Killer



In the Linux Kernel, there is Out-of-Memory (OOM) Killer process which can kick in and kill processes when the system runs dangerously low on RAM.

  • The OOM killer may or may not kill the process that is consuming all of the RAM.
  • It’s also possible OOM killer may kill another process instead of the real culprit. So then in these cases, you would find yourself rebooting the system to ensure the system processes are running.


If the OOM killer kicks in, you can see this error in /var/log/syslog or /var/log/messages:

1228419127.32453_1710.hostname:2,S:Out of Memory: Killed process   21389 (java).


Learn more about OOM Killer: https://dev.to/rrampage/surviving-the-linux-oom-killer-2ki9


Page Allocation Failure