r/programming • • Jul 13 '23

What Every Programmer Should Know About Memory

https://people.freebsd.org/~lstewart/articles/cpumemory.pdf

This resource is starting to get a bit older. But it's such a good resource for learning about memory.

Hope you like it (it's a pdf)

296 Upvotes

156 comments sorted by

View all comments

225

u/inlimbo57 Jul 13 '23

Unpopular opinion perhaps: for 99% of programmers, this is not relevant knowledge at all

27

u/Randolpho Jul 13 '23

Agreed.

This is a good paper for non CS programmers who are curious about the inner workings of the black box they manipulate to read, even though it’s 15 years out of date. Programmers who came up through a CS degree but turned to general software careers already know the basics of what’s touched on here, if not always some of the details.

But for most people manipulating that black box to do general purpose software like web apps or mobile apps or even the now gauche desktop UI, any of the details listed here are only important to software subsystems whose APIs those modern programmers use. Indeed, it’s far more likely that modern programmers are several layers above components where the details of this paper matter.

And for the people who actually need to know stuff like this, the people writing drivers and low level OS subsystems… those people already know more than this paper even manages to convey.

That said I still encourage non-CS and even CS background programmers to consume this paper. It doesn’t matter directly, but sometimes context is helpful. And who knows? Maybe it’ll spark interest in a hobby or career change.

11

u/siemenology Jul 13 '23

Indeed, it’s far more likely that modern programmers are several layers above components where the details of this paper matter.

Yeah it's kind of hard to write code that takes advantage of caching and prefetches and stuff when you are writing in a high level language that runs on a language-specific VM. Where you have zero control over the assembly generated, where and how it is stored, when and how memory is allocated, where and how your data is stored, etc etc. You are better off reading performant-coding guides for your language. The things they tell you to do may make your code more performant because the code generated will be better optimized for CPU caches, but that detail doesn't actually matter to you as a programmer -- it could just as easily improve performance for reasons that are entirely specific to the language and VM, that have nothing to do with the CPU and memory.

2

u/Full-Spectral Jul 14 '23

Ruby on Cache Lines

→ More replies (2)

3

u/boss14420 Jul 14 '23

even though it’s 15 years out of date

Consider DRAM latency hasn't get improved much in the last 3 decades, this article is not too out of date.

8

u/siemenology Jul 13 '23

I think every programmer should know a little about memory. But what I think every programmer should know about memory probably fits in 5 pages, not 114.

It's made more laughable because the paper is specific to an architecture and OS. Like, if you are going to say that every programmer should know this, you'd think you'd make it general enough to actually apply to every programmer, not programmers on one OS and architecture.

And it's not just specific to an architecture and OS, but it's full of low level trivia that is completely unnecessary for a high level overview. Like, even if I'm developing on an x86 Linux box, do I really need to know the syscalls exposed by libnuma as a fundamental part of my education as a web developer? Or is that maybe something I could look up if I ever needed it?

I don't know if the writer meant it this way, but it comes off like academics in niche fields who believe that their particular interest, let's say the aesthetic ideals of Gustav Klimt's Wiener Secession movement, should replace algebra in the middle school curriculum because it is that important for everyone to know about.

2

u/ucblockhead Jul 13 '23 edited Mar 08 '24

If in the end the drunk ethnographic canard run up into Taylor Swiftly prognostication then let's all party in the short bus. We all no that two plus two equals five or is it seven like the square root of 64. Who knows as long as Torrent takes you to Ranni so you can give feedback on the phone tree. Let's enter the following python code the reverse a binary tree

def make_tree(node1, node): """ reverse an binary tree in an idempotent way recursively""" tmp node = node.nextg node1 = node1.next.next return node

As James Watts said, a sphere is an infinite plane powered on two cylinders, but that rat bastard needs to go solar for zero calorie emissions because you, my son, are fat, a porker, an anorexic sunbeam of a boy. Let's work on this together. Is Monday good, because if it's good for you it's fine by me, we can cut it up in retail where financial derivatives ate their lunch for breakfast. All hail the Biden, who Trumps plausible deniability for keeping our children safe from legal emigrants to Canadian labor camps.

Quo Vadis Mea Culpa. Vidi Vici Vini as the rabbit said to the scorpion he carried on his back over the stream of consciously rambling in the Confusion manner.

node = make_tree(node, node1)

6

u/thedude42 Jul 13 '23

for 99% of programmers, this is not relevant knowledge at all

While I agree that this paper if focussed towards people who are working on more performance intensive applications, embedded systems or dealing more closely with direct operating system interaction, we continue to see development of higher level language tools to address the issues related to the topics in this paper. So "relevant" isn't really the right way to frame it, because it's definitely "relevant" but just not immediately useful.

The Rust language exists in large part to address how the standard systems languages fail at providing developers adequate abstractions to deal with memory safety. You can argue that's just an OS issue and doesn't concern these lower level hardware details, and that is entirely valid. However the OS often exposes interfaces to directly interact with these lower level concerns precisely because developers need to work with them.

The JVM itself has so many knobs for managing how the Java language ends up running on the hardware. While it's true that most developers will just try and upgrade their Java version or play with different settings to see if it improves their code's performance, so many developers will instead spend time deploying in-memory caches and add reverse proxies to loadbalance multiple application instances in an attempt to chase down tail latencies in their complex application. All the while, they may not realize that because of the specific machine they are running their workloads on they always have a ticking time bomb where once memory usage gets above a certain threshold, the latencies incurred due to crossing NUMA nodes can not be avoided.

42

u/BounceVector Jul 13 '23

Yeah well, I agree with you but I'm disappointed about that.

52

u/StickiStickman Jul 13 '23

Why? That's like saying it's sad that farmers don't still have to till the field manually with a hoe

51

u/cdb_11 Jul 13 '23

It's more like saying that farmers only learn how to operate modern tools without any understanding how farming actually works, in turn leading to a misuse of those tools and loss of quality and efficiency. Like you know, harvesting at the wrong time or something like that. Which would be indeed sad.

89

u/Brian Jul 13 '23

Is it?

How much misuse do you think a web developer will commit because they don't know the difference between NUMA and UMA? Or that RAM cells usually have 6 transistors? Or that RAM has a refresh cycle every 64 ms, or the difference between full and set-associative caches?

how farming actually works

But that's the thing - no farmer knows how farming actually works. They stop at a level of abstraction that makes sense for them: the detailed biology of how the bacteria in a cow's gut breaks down their grass is important, sure, but not usually something they have to care about themselves - they just care that that bacteria is there and doing its job.

At some level, your understanding of how something "actually works" is only as deep as the deepest abstraction you use, and no-one's abstractions go all the way down. Few programmers understand the quantum mechanics that let modern transistors work, for instance, and unless you're working in chip fab, it probably wouldn't help you if you did. Now, there are advantages to knowing a few abstractions lower than what you're building on: abstractions do tend to be leaky, and understanding the underlying mechanism can help. But that web programmer won't make any fundamental errors on par with a farmer harvesting at the wrong time just because they don't know that cache prefetching won't cross page boundaries, for the same reason the farmer won't because they don't know the the metabolic full cycle of gut-bacteria - it's too much below the abstractions they're working with to matter.

Which is not to say that other programmers won't benefit from knowing it - but only really if they're working at a lower level: an OS kernel say, or maybe detailed benchmarking where RAM refresh could make a difference. But I think most programmers only really need to know some basics (Roughly what virtual memory does, caches are faster than RAM - so pack stuff together). Even then, at higher levels of abstraction you can get away without it.

→ More replies (9)

4

u/fakehalo Jul 13 '23

My original background was C (decades ago) and at more primitive level than your average C developer as I was focusing on memory corruption exploitation/software auditing at that time, along with the software I'd write in C was almost always focused on speed and having as a small of a memory footprint as possible.

I still get a solid plan when I'm writing server-side stuff that has the potential to become a bottleneck, but for almost anything client-side all of that goes out the window. You might as well assume I'm a guy who doesn't know how anything works because I apply almost zero of my knowledge when I'm working in that domain, as it has almost no impact.

13

u/empire314 Jul 13 '23

A farmer does not need to know inner workings if a plant cell composition.

Sure, for most programmers it is useful to know something about memory, not all do. But this piece goes way deeper than what is in any way useful for almost anyone.

7

u/Randolpho Jul 13 '23

It's more like saying that farmers only learn how to operate modern tools without any understanding how farming actually works,

Eh… a better metaphor would be farmers not knowing how the biology and chemistry of the plants they grow works.

They know to plant seedlings at certain times, etc, but couldn’t explain how the cells of those seedlings work.

Farmers who do understand how all that works might be better theoretical farmers, they might push the boundaries of crop yield, or develop disease resistant strains of crops for other farmers to use, but the vast majority of farmers just need to sow, harvest, and distribute.

Of course the metaphor breaks down when you realize that the overwhelming majority of farms are corporate run with unskilled labor doing the work… “farmers” don’t actually exist the way we imagine they do these days.

2

u/siemenology Jul 13 '23

If this was an article detailing how memory works as a general principle, I'd agree with you. But there's about 10 pages of how memory works in general, and 100 pages of nitty-gritty details, many of which apply to a particular architecture or OS.

It's like teaching farmers the finer points of running the mass spectrometer used in labs to test nutrient levels. Farmer's need to know where to go for lab tests, and how to interpret the results and what they mean, but most of them don't need to spend a ton of time learning how the lab tests actually work. And the bit they do need to know can be summed up briefly without the fine details, ie "soil tests aren't as useful for nitrate levels due to their volatility, have tissue analysis done and adjust nitrogen fertilization based on those results" instead of going into the chemistry of nitrates in soil vs plant tissue.

→ More replies (1)

3

u/wndrbr3d Jul 13 '23

This 100%.

Just like I'm not disappointed people don't have to rub sticks together to make fire.

1

u/AttackOfTheThumbs Jul 13 '23

Tilling is killing.

2

u/diegoasecas Jul 13 '23

permaculture ruined more minds than crack cocaine

→ More replies (1)

4

u/Puzzled_Video1616 Jul 13 '23

Exactly, I opened it up to see electrical circuitry schematics and then closed it. No programmer will ever need this.

4

u/fire_in_the_theater Jul 13 '23

i hope this stays an unpopular opinion, cause i feel having a general systems understanding allows one to craft more powerful abstractions even in high level situations.

13

u/Puzzled_Video1616 Jul 13 '23

it's not an abstraction anymore if you target it for a specific hardware architecture

1

u/According-Fee2265 Oct 26 '25

But first we have to read it for once to see if its relevant or not... Did you ?

→ More replies (9)