Sign in

Follow what people learn, not everything they post.

Engineers, mathematicians, historians and language learners post short kukis about what they’re learning, kept in tracks. Sign in to follow just the tracks you want.

Short kukis from people learning anything, kept in tracks. Follow just the tracks you want.

Sign in to follow How it works Free to read. Posting is by invite. How it works

In my previous post, I talked about how a virtual address is structured. To recall, a virtual address has 2 parts: virtual page number (VPN) and the offset.

We also mentioned that the OS keeps a per-process data structure called page table. For now just imagine it as a list of page table entries (or PTEs).

When the process is running, the OS stores address of the page table in the page table base register (PTBR). So to find the page you're looking for, all you need to do is:

\(ThePage = PTBR + (VPN * sizeof(PTE))\)

Reading from a memory address isn't easy. Here's why:

A memory address consists of 2 parts: virtual page number (VPN) and the offset. The VPN tells you on which page does this address live, and the offset helps you find your value inside that page.

When a process makes memory access, the OS needs to convert that virtual address to a physical one. So, it first needs to, extract VPN from your address, then find physical page number (PPN) which corresponds to that VPN. And to do that, it needs to access per-process data structure called page table which resides in RAM.

As you might have already noticed, it's really slow. If the CPU wants to read a value from an address, it needs to go to memory 2 times: one for address translation, one for reading the value itself.

Describe the image

React Mostly interesting
Save

So, you have heard of garbage collection and you roughly know that it is mainly for the stuff that gets allocated on the heap. But what is heap, really?

When a process is created, the OS will allocate memory for it, which we call process' virtual address space (VAS). The VAS has different sections where OS stores process' code (instructions), initialized/uninitialized data, stack etc. The stack will grow and shrink when you call or return from functions.

Virtual address space of a program

React Mostly dope
Save

Linux has a mechanism called OOM killer (out-of-memory killer) which is used to kill processes when the memory is critically low. It is usually triggered when a memory allocation fails or when a process hits its cgroup limit.

It turns out you can influence its decision to kill or not to kill you. It decides by a factor called badness score which you can set inside /proc/[pid]/oom_score_adj file. You can also view your process' OOM score by looking at /proc/[pid]/oom_score file. A higher oom_score_adj makes a process more likely to be killed.

For instance, as you might already know, chromium uses a separate renderer processes for each tab. Chromium sets higher oom_score_adj values for its renderer processes so that when OOM killer goes on a killing spree, it first kills the renderer processes.

React Mostly sweet
Save

Newest first, from every topic. Sign in to follow just the tracks you want.