Ben Chuanlong Du's Blog

It is never too late to learn.

Memory Layout in Rust

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

Different Types of Memory in Rust

Rust has 3 different types of memory: static memory, stack memory and heap memory.

  1. Static variables live in static memory and is determined at compile time. It is suggested that you define large data variables as static so that they live in the static memory instead of stack memory to avoid stack overflow problems. Of course, another way is to put those variables into heap memory (but at the cost of slight performance loss).

Tips on Valgrind

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

Memory Layout of Enum in Rust

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

Tips & Traps

  1. The closest thing to describe Rust Enum is tagged Union. The Rust compiler adds an extra (up to) 8 bytes to the enum to store the discriminator. This is used to identify the variant currently stored in the enum. However, Rust does NOT guarantee that the memory layout of an enum is always a tag followed by a union. Instead, the Rust compiler retains the freedom to optimize the layout of types so that the code can be more memory efficient.

Get Total Physical Memory in Python

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

Using os.sysconf

Notice that this ways only works on Linux but not on macOS or Windows.

Get physical memory in bytes.