Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between RSS and heap?

In the context of Node.js, I often come across mentions of RSS memory. The Wikipedia article on RSS is pretty terse and I'm still quite unsure how it differs from heap memory. Is it the heap + stack? Could someone explain it to me like I was 5 years old?

like image 450
Olivier Lalonde Avatar asked Jun 29 '13 14:06

Olivier Lalonde


2 Answers

A process is given memory to run in by the operating system. On modern 32 bit and 64 bit systems, this memory space looks like a wide open field of all possible memory addresses that can be addressed. In reality, the operating system is "lying" to the process, and can often back this commitment with only part of the memory that can be potentially addressed by the process. The rest is "virtual".

Since the CPU can only execute on and from data and code in RAM, the OS works with the CPU to keep track of what memory the program is using is in RAM and what is saved to a special file on the disk in virtual RAM (i.e. the pagefile/swapfile). What is in RAM is called the "resident" or "working" set.

This is important to know as a developer since the time it takes to access memory that is already in RAM is many orders of magnitude faster than if the OS has to load the memory from disk into RAM first. Designing programs that keep critical data resident have higher performance characteristics than programs that are careless about how memory is allocated and accessed.

The heap is partitioning of the wide open memory the OS presents to a process, organized into a data structure bearing the same name. The process uses this organization as it executes to access (and track for later release) only small chunks at a time. For example if the process wants to store an array of integers, it needs a chunk of memory with as many bytes as there are elements times the size of the integer.

This partitioning is over top of what the OS does with swapping. I access the heap to store allocations of bytes of memory for my objects and data structures at runtime, but this heap allocation still lives in the memory the OS gave me, which it looks at in terms of 4096 byte "pages" which can be moved back and forth from disk.

The stack is another special data structure that the OS gives a process but it is different in that it gets all the stack at once, and as a process puts items on the stack in sequence, it increments (or decrements) a special pointer (usually a special CPU register) to keep track of where it is in this stack. At a higher level, each thread keeps track of a stack and where the pointer is in that stack. Local variables, function parameters and return pointers are stored here and the pointer is incremented and decremented over this memory as the process executes to keep track of these things.

like image 55
codekaizen Avatar answered Oct 22 '22 07:10

codekaizen


Generally, what application level programmers call memory is really just an address space. So the heap, the stack, even the program segments are just sets of address. Our programs, including those in node, read and write data using those addresses. We call things like malloc a "memory manager" but it really should be "address manager." A separate "virtual memory" system determines whether or not those address sets are mapped to RAM, disk, or nothing at all. The resident set is those addresses that are backed by RAM. The resident set size is just how big that set is.

like image 24
DrC Avatar answered Oct 22 '22 07:10

DrC