How virtual memory works on your phone

How virtual memory works on your phone


Virtual memory is a fundamental concept in all modern operating systems, including Android. It’s often misunderstood as a trick or a gimmick to make users think their devices have more RAM than they do. Even the most powerful and best mobile phones available today use virtual memory.




Marketing buzzwords like Virtual RAM or RAM Plus can add to the confusion. These terms suggest that virtual memory is more like a bonus feature than a standard approach to how operating systems manage memory. Understanding how virtual memory works on Android provides insights into how memory is managed on mobile devices and how they perform under heavy memory loads.


A brief history

The concept of virtual memory was introduced in 1959 at the University of Manchester computer lab as part of a joint project with Ferranti Computers and Plessey. It was integral to the famous Atlas Computer system, which was the fastest computer in the world at the time, mainly due to the novel memory management system it developed.


At the time, managing and planning how data moved between the computer’s main memory (RAM) and its secondary storage (like hard drives) required programmers to code the transfers every time a change was needed. This could be time-consuming and frustrating because the computer couldn’t perform other tasks while this occurred.

Virtual memory introduced a memory management system that automated much of this process. It allowed programmers to code applications as if they had access to all the computer’s primary memory without worrying about how else it was allocated.

This was done using the secondary storage to map virtual memory addresses stored in its memory to physical addresses in the primary memory. When the virtual address needed to be transferred to physical memory, it was done through system control, which could be considered one of the first operating systems. On Android, the kernel takes care of essential system tasks like this.


Related

Everything you need to know about your phone or tablet’s kernel

It’s one of the most important aspects of any device

Understanding virtual memory through a library analogy

Imagine you’re at a library. You’re sitting at a desk and doing research. You’re limited to the number of books you can have on your desk at any time, but there’s a whole building filled with books all around you. Everything in front of you, the desktop and the books you have at your immediate access, is like your device’s RAM. It’s the data that’s available to you right now, with little effort involved in retrieving it because it’s visible and within arm’s reach.

You know there are other books in the library you’ll need to reference to finish your research. You’re not exactly sure where they are or what shelves they are on, but they contain information you will use.


Before virtual memory

Before virtual memory, when you needed another book from a bookshelf in the library, you had to get up, find it on the shelves, and bring it back to your desk. If you ran out of space on your desk, you had to decide which books to put back on the shelves to make room for the new ones, even if you weren’t done referencing them. This process was time-consuming and frustrating, especially if you had to keep retrieving the same books.

Enter virtual memory

Virtual memory is like having your own personal shelf (hard drive or SSD storage) next to your desk and a library assistant ready to get the book you need at any moment. Instead of putting a book back on the original shelf it came from, you can put it on your personal shelf so that it’s easily accessible. This is like the memory mapping technique used for virtual and physical addresses, called a page table.

When you need a book, all you do is ask. The library assistant brings it to your desk, grabbing another book from your desk to place on your personal shelf if you’re running out of space. The library assistant always knows what books are on your personal shelf and can retrieve a book from other shelves in the library if needed. This process happens seamlessly and invisibly, so you can focus on your research without interruptions.


Address vs. physical location

To understand how clever the solution to this problem was, we need to look at how the very concept of a memory “address” was changed by the team at the University of Manchester. Their idea, called one-level storage, is now known as virtual memory. It gives the programmer the illusion that they have a large primary memory (RAM) at their disposal, even though the computer has a relatively small main memory.

At the heart of their idea is the notion that “address” is distinct from “physical location.” In our library analogy, think of an address as the spot on your desk where you place a book to read. In the traditional sense, this spot is linked to the physical space on your desk. Without virtual memory, if your desk is full, you physically can’t place another book there without removing one first.


With virtual memory, the concept of an address changes. Your address (the spot on your desk) can now refer to any book in the library, regardless of where it is physically stored. It’s as if the library assistant remembers exactly where every book is and can bring it to your desk when you need it, without you having to know or care about its physical location in the library.

How virtual memory works on Android

Virtual memory on Android operates similarly to the library analogy we discussed. However, in a mobile operating system, it involves complex software and hardware mechanisms to manage memory efficiently.

In Android, as in other operating systems, a memory address is a way for applications to reference memory locations. Because of virtual memory, these addresses don’t have to correspond to physical locations in the device’s RAM. Instead, Android uses virtual memory to give each application the illusion of having its own large, continuous block of memory by leveraging additional secondary storage space.


This allows applications on Android to isolate themselves from one another, providing an additional layer of security and preventing one application from interfering with the memory of another. It also simplifies the development process because developers need to worry less about physical memory limitations and don’t need to code their own memory management. The operating system does it for them.

Related

Android 15 could help apps load webpages faster

A change to WebView should help speed up the browsing experience

Virtual memory terms and processes

Paging:

  • Virtual pages: The operating system divides the allocated virtual address space into blocks called pages.
  • Page table: Each application has its own page table that maps these virtual pages to physical pages in the device’s physical RAM.
  • Translation: When an application needs to access memory data, the Memory Management Unit (MMU) translates the virtual address to the corresponding physical address using the page table.

Swapping:


  • Swap space: Android devices typically avoid traditional swapping due to performance reasons. However, Android devices can use something called zRAM, a compressed form of swap space that resides in the physical RAM.
  • zRAM: This compresses data similarly to a .zip or .rar archive, increasing the available memory by reducing the size of inactive pages that are loaded into the physical RAM.
  • zRAM process: This can impact performance because when compressed data is called for, it needs to be uncompressed before it can be accessed.

Low Memory Killer (LMK):

  • Process management: Android uses the Low Memory Killer to manage memory more aggressively than traditional desktop or laptop systems. When memory is low, LMK terminates background processes to free up RAM for active applications.

Benefits of virtual memory on Android

Isolation and security: Each application runs in its own sandboxed memory environment with its own virtual memory space. This isolation ensures that one application cannot access the memory of another, enhancing security and preventing performance interference.


Efficient memory utilization: Virtual memory allows Android to run more applications simultaneously by efficiently managing the available physical memory. Applications can stay open in the background and allow only essential background processes to run. Pages that are not actively used can be compressed or swapped out to make room for current or more critical tasks.

Performance considerations

While virtual memory provides significant advantages, it also has some performance implications:

Page faults: Sometimes, an application can request access to a virtual page that is not currently mapped to a physical page, and a page fault occurs. The operating system has to retrieve the data from zRAM or storage, which can cause a delay.

Memory compression: zRAM helps increase the effective memory capacity of physical RAM but can introduce latency as data needs to be compressed and decompressed.

Process termination: The Low Memory Killer’s aggressive memory management can sometimes lead to the premature termination of background processes, impacting the user experience when switching between apps since those processes need to start up from scratch again.


Adaptive memory policies: Implementing intelligent memory management policies that dynamically adjust based on current system usage and workload, optimizing performance and resource utilization.

Is virtual memory still needed with fast SSDs?

Even with the advent of superfast SSD drives and new types of storage, virtual memory is still an important part of modern operating systems, including Android. Combining fast SSDs with traditional RAM is faster than slower physical hard drives, but RAM is faster than an SSD.

While fast SSDs reduce the latency of accessing secondary storage, they do not eliminate the need for virtual memory. However, when a page fault occurs, they decrease the time it takes to retrieve data from secondary storage. This can improve system performance but doesn’t replace the need for virtual memory. Faster secondary storage also increases swap performance, making the system more responsive when it needs to free up RAM by offloading less critical processes to the SSD.


Optimize your Android with virtual memory

If you search for information about virtual memory on Android, you’re bound to run into people suggesting you limit the amount your device can use or, in some cases, turn it off. For most users, leaving virtual memory activated is the way to go unless a manufacturer has a problem with its implementation of virtual memory, like Samsung’s RAM Plus. Even those problems are bound to be fixed. Turning off Ram Plus should be considered a temporary option.

The concepts behind virtual memory have been around for more than half a century, and modern implementations are designed to optimize performance and multitasking, enhance security and stability, and provide developers with a simplified programming model. Virtual memory ensures efficient use of primary memory resources, while fast SSDs reduce the latency of accessing secondary storage.



Source link

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *