<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Memory on Shai Asher</title><link>https://slow-is-smooth.io/tags/memory/</link><description>Recent content in Memory on Shai Asher</description><generator>Hugo</generator><language>en-gb</language><lastBuildDate>Sun, 21 Jul 2024 09:00:00 +0300</lastBuildDate><atom:link href="https://slow-is-smooth.io/tags/memory/index.xml" rel="self" type="application/rss+xml"/><item><title>eBPF Learns to Allocate</title><link>https://slow-is-smooth.io/blog/ebpf-learns-to-allocate/</link><pubDate>Sun, 21 Jul 2024 09:00:00 +0300</pubDate><guid>https://slow-is-smooth.io/blog/ebpf-learns-to-allocate/</guid><description>&lt;p&gt;On Linux 6.10, a sleep command ran for a tenth of a second, exited and was reaped by its parent,&#10;and the kernel duly reported that no process with its ID existed any more. A BPF map still held&#10;the kernel&amp;rsquo;s record of it, the task structure, with a reference count of exactly one: the map&amp;rsquo;s&#10;own. A red-black tree, built by a BPF program out of objects it had allocated itself, named the&#10;five most CPU-hungry of ten processes, in order. The loop from Part 4 that had spent the&#10;verifier&amp;rsquo;s entire budget of a million instructions loaded after 42, with one line added. And a&#10;user-space program walked a million-node linked list that a BPF program had built, following&#10;ordinary pointers, in about 2.2 milliseconds; through map lookups, the same walk took about 370.&lt;/p&gt;</description></item><item><title>One Program, Seven Runtimes</title><link>https://slow-is-smooth.io/blog/one-program-seven-runtimes/</link><pubDate>Sun, 07 Mar 2021 09:00:00 +0200</pubDate><guid>https://slow-is-smooth.io/blog/one-program-seven-runtimes/</guid><description>&lt;p&gt;In the last part of this series on Go&amp;rsquo;s stack and heap, eight versions of one program build the same&#10;data: a table of a million entries, a binary tree of two&#10;million nodes, and 65 million short-lived nodes that are built and thrown away. In Go 1.16 a goroutine&#10;that sleeps for a millisecond at a time wakes up to 18 ms late while that happens; in Java with ZGC, 1.2&#10;ms; in CPython, 0.7 seconds, because Python&amp;rsquo;s cycle collector stops everything to look at three million&#10;objects, 86 times. When the work is done and everything is dropped, Go 1.16 hands back 260 MB within&#10;half a minute, and the same program built with Go 1.15 never does as far as &lt;code&gt;top&lt;/code&gt; and RSS can tell,&#10;although its runtime released the memory just as promptly.&lt;/p&gt;</description></item><item><title>Go on Windows: Where the Stacks Come From</title><link>https://slow-is-smooth.io/blog/go-on-windows-where-the-stacks-come-from/</link><pubDate>Sun, 06 Sep 2020 09:00:00 +0300</pubDate><guid>https://slow-is-smooth.io/blog/go-on-windows-where-the-stacks-come-from/</guid><description>&lt;p&gt;Run the same Go program on Linux and on Windows and its goroutines are not the same size. On Windows&#10;every goroutine starts with an 8 KB stack, four times what it gets on Linux, because Windows writes the&#10;record of a processor exception onto whatever stack the fault happened on. A gigabyte that a Go program&#10;allocates and never touches costs a full gigabyte of Windows&amp;rsquo; commit charge, and on a machine with 4 GB&#10;of memory the ninth such gigabyte stops the program, where Linux let the same program reach 64 GB on a&#10;31 GB machine. And a C function, called from Go, that runs out of stack leaves, nine times in ten, a&#10;process that prints its crash report and then never exits.&lt;/p&gt;</description></item><item><title>Memory the Runtime Keeps</title><link>https://slow-is-smooth.io/blog/memory-the-runtime-keeps/</link><pubDate>Sun, 06 Oct 2019 09:00:00 +0300</pubDate><guid>https://slow-is-smooth.io/blog/memory-the-runtime-keeps/</guid><description>&lt;p&gt;A Go 1.13 program builds up a gigabyte of heap, drops it, and collects the garbage. Three minutes&#10;later the runtime reports that it has given all of that gigabyte back to the operating system. The&#10;kernel still counts every byte of it against the program: &lt;code&gt;top&lt;/code&gt; shows 1,058 megabytes resident, and&#10;it goes on showing 1,058 for as long as anyone watches. Then another program on the same machine asks&#10;for memory, and within seconds the Go program&amp;rsquo;s resident set drops to 38 megabytes, without the Go&#10;program doing anything at all. Both the runtime and &lt;code&gt;top&lt;/code&gt; were telling the truth.&lt;/p&gt;</description></item><item><title>A Collector That Never Moves a Thing</title><link>https://slow-is-smooth.io/blog/a-collector-that-never-moves-a-thing/</link><pubDate>Sun, 13 Jan 2019 09:00:00 +0200</pubDate><guid>https://slow-is-smooth.io/blog/a-collector-that-never-moves-a-thing/</guid><description>&lt;p&gt;Make 3.2 gigabytes of short-lived garbage in a Go program, and its garbage collector spends 52&#10;milliseconds of CPU cleaning up after it. Or 3,054 milliseconds. The garbage is identical in both&#10;runs. The difference is what else the program is holding: half a gigabyte of plain numbers costs the&#10;collector almost nothing, and half a gigabyte of objects that hold pointers costs it sixty times as&#10;much. Java&amp;rsquo;s collectors spend a few milliseconds on the same garbage, whichever it is, and .NET gets&#10;through all of it in a quarter of a second. Go&amp;rsquo;s collector does not mind how much garbage there is. It minds what is still alive, and it&#10;looks at all of it, every cycle.&lt;/p&gt;</description></item><item><title>Where the Memory Goes</title><link>https://slow-is-smooth.io/blog/where-the-memory-goes/</link><pubDate>Sun, 11 Nov 2018 09:00:00 +0200</pubDate><guid>https://slow-is-smooth.io/blog/where-the-memory-goes/</guid><description>&lt;p&gt;A Python 3.7 process makes four million floats, 130 MB of resident memory, and then deletes all but&#10;one in every thousand. Four thousand floats are left, about 96 kilobytes of actual data. The process&#10;still uses 100 MB. Delete those last four thousand and it drops to 7 MB at once. Nothing leaked: the&#10;memory was in use, by the allocator&amp;rsquo;s bookkeeping rather than by the program, because CPython hands&#10;memory back to the operating system only in 256 KiB pieces, and only when every object in a piece&#10;is gone.&lt;/p&gt;</description></item><item><title>The Allocator Under Every new</title><link>https://slow-is-smooth.io/blog/the-allocator-under-every-new/</link><pubDate>Sun, 07 Oct 2018 09:00:00 +0300</pubDate><guid>https://slow-is-smooth.io/blog/the-allocator-under-every-new/</guid><description>&lt;p&gt;Ask Go&amp;rsquo;s heap for 33 bytes and it hands back 48. Ask for 32,769 and it hands back 40,960. Keep&#10;every second one of a million small integers and you free nothing at all. And until August, a C&#10;library that mapped 64 kilobytes of memory in the wrong place could kill a Go program as soon as its&#10;heap grew into that place, with an error that blamed neither. Go 1.11, released on 24 August,&#10;fixed the last of these by changing where the heap lives. The others are how Go&amp;rsquo;s allocator is&#10;designed, and they are worth knowing.&lt;/p&gt;</description></item><item><title>Counting References</title><link>https://slow-is-smooth.io/blog/counting-references/</link><pubDate>Sun, 16 Sep 2018 09:00:00 +0300</pubDate><guid>https://slow-is-smooth.io/blog/counting-references/</guid><description>&lt;p&gt;A parent process builds a list of a million small lists, 184 MB of objects, and forks. The child&#10;never assigns to the list. It only loops over it and reads one number from each element, and when&#10;it&amp;rsquo;s done, 93 MB of the parent&amp;rsquo;s memory has been copied into the child. Reading a Python object&#10;writes to it, because every reference taken and dropped changes the object&amp;rsquo;s reference count, and&#10;the reference count lives in the object. The kernel can&amp;rsquo;t tell a reference count from data. It sees&#10;a write to a shared page and copies the page.&lt;/p&gt;</description></item><item><title>The Compiler Decides What Escapes</title><link>https://slow-is-smooth.io/blog/the-compiler-decides-what-escapes/</link><pubDate>Sun, 24 Jun 2018 09:00:00 +0300</pubDate><guid>https://slow-is-smooth.io/blog/the-compiler-decides-what-escapes/</guid><description>&lt;p&gt;Add one &lt;code&gt;fmt.Println&lt;/code&gt; to a Go function that never allocated, and every call to it now allocates.&#10;Nothing in the source says where a variable lives. Go has no &lt;code&gt;new&lt;/code&gt; that means &amp;ldquo;heap&amp;rdquo; and no&#10;declaration that means &amp;ldquo;stack&amp;rdquo;. The compiler decides, one variable at a time, by working out&#10;whether anything could still refer to the variable after its function returns. If something could,&#10;the variable &lt;em&gt;escapes&lt;/em&gt; and goes on the heap. If nothing could, it stays in the stack frame and costs&#10;nothing to free. The debugging line made a variable escape, and the compiler moved it without&#10;saying a word.&lt;/p&gt;</description></item><item><title>Every Value Is a Pointer to a Struct</title><link>https://slow-is-smooth.io/blog/every-value-is-a-pointer-to-a-struct/</link><pubDate>Sun, 13 May 2018 09:00:00 +0300</pubDate><guid>https://slow-is-smooth.io/blog/every-value-is-a-pointer-to-a-struct/</guid><description>&lt;p&gt;On a freshly installed Ubuntu 18.04, the Python integer 1000 is 28 bytes of memory at an address&#10;you can print. The first eight bytes count how many things refer to it. The next eight are the&#10;address of a C structure that describes what an integer is. Eight more say how many 30-bit digits&#10;follow, and the last four hold the number itself, as the bytes &lt;code&gt;e8 03 00 00&lt;/code&gt;. Python goes to some&#10;lengths so that you never have to know any of this. It does not always succeed, and the places&#10;where it fails are the subject of this series.&lt;/p&gt;</description></item><item><title>A Goroutine Starts with Two Kilobytes</title><link>https://slow-is-smooth.io/blog/a-goroutine-starts-with-two-kilobytes/</link><pubDate>Sun, 06 May 2018 09:00:00 +0300</pubDate><guid>https://slow-is-smooth.io/blog/a-goroutine-starts-with-two-kilobytes/</guid><description>&lt;p&gt;A C program that starts ten thousand threads on Linux reserves 80 gigabytes of address space for&#10;their stacks, 8 MB each, before any of them has done anything. A Go program that starts a hundred&#10;thousand goroutines spends about 2 KB of stack on each. Yet any one of those goroutines can recurse&#10;half a gigabyte deep before Go stops it. Both are true because a goroutine&amp;rsquo;s stack does not stay&#10;where it is: when it fills, the runtime allocates a new one twice the size, copies every frame&#10;across, and rewrites every pointer that pointed into the old one.&lt;/p&gt;</description></item></channel></rss>