
Three kinds of memory leaks - troydavis
https://blog.nelhage.com/post/three-kinds-of-leaks/
======
ninkendo
This reminds me of a horrible hack I once put into an internal ruby (sinatra)
REST API server that had to aggregate data from a ton of SOAP servers in a
pretty large environment, and return some computed results as JSON. The amount
of XML that had to be parsed into objects to do the reporting was massive, and
the web workers (being ruby) never gave the memory back to the OS. Running the
code inside a unicorn process would basically cause it to start using several
GB of ram, and over time all the unicorn workers would become bloated and the
service would have to be restarted.

I eventually decided to just fork() the ruby process, do the processing,
compute the JSON results (which were comparatively small) and hand the results
back as a string over a socket to the parent process, which would cache them
and respond. It pretty much solved the memory issue, since the forked process
would just exit() gave all the memory back.

In fact, I think I ended up doing a GC.disable() inside the forked process
(because I was going to exit anyway) and it sped the process up from taking >2
minutes to just a few seconds. Ruby's GC was utterly terrible back then.

It was an internal tool that never was touched by the general internet, which
is why we could "afford" to have it do such intensive work behind a single
request, but I always wonder if there was a better approach that wasn't so
awful.

~~~
pjc50
Ah, the "missile" approach to memory management:
[https://groups.google.com/forum/message/raw?msg=comp.lang.ad...](https://groups.google.com/forum/message/raw?msg=comp.lang.ada/E9bNCvDQ12k/1tezW24ZxdAJ)

~~~
yread
hmm www.rational.com is broken, but is that the Rational from Rational Unified
Process?

~~~
pjc50
Yes. And, may god have mercy on its soul, Rational ClearCase. One of the more
unhelpful proprietary version control systems.

[https://en.wikipedia.org/wiki/Rational_Software](https://en.wikipedia.org/wiki/Rational_Software)

------
pietroglyph
For the third type of "leak" (GC frees memory from the program, but never
returns it to the OS) the article says that this is often an issue for Python
and Ruby... Does this happen in other languages with a GC like Java or Go, or
are Ruby and Python the only ones/especially bad?

~~~
merb
the JVM _NEVER_ returns memory to the os. but that is not a problem since
memory can always be reused on the jvm.

I wouldn't consider the third category a memory leak.

~~~
cryptonector
A cynic might say that it's not a problem because if you're running the JVM
then that's probably all you're running...

~~~
gowld
JVM has -Xmx for choosing how much memory you want it to use.

------
seandougall
I’m surprised I don’t see reference cycles listed. Strictly speaking I suppose
they’re a subset of “unreachable allocations”, but they have a different feel,
particularly as they can occur even with GC.

~~~
hawkice
Garbage collection can handle reference cycles -- that's why it is better than
reference counting (at handling the eventual freeing of arbitrary data).

------
eecsninja
I've encountered type 2 leaks while working on a popular web browser. It
wasn't type 1 leak and so it got overlooked.

One example: on pages that self-refreshed at regular intervals, the same page
URL was added to the history list each time. That ended up leaking a ton of
memory if you left the page open over several days.

------
RyanRies
Does anyone else see any irony in that you still spend so much energy
debugging memory leaks in garbage-collected environments?

~~~
seandougall
Sort of. I don't know if I'd call it irony, exactly—the physical metaphor
would perhaps be that the garbage truck comes by once a week but I still have
to remember to take the bins out to the curb. GC definitely gets overhyped for
beginners, though. Most docs/tutorials "forget" (see what I did there?) to
mention memory management until the situation becomes so complex that it's
unavoidable, by which time the concept is unnecessarily difficult to map onto
established patterns.

------
banachtarski
[referring to unreachable allocations]

> If you’re writing in C or C++, especially C++ without ubiquitous use of
> smart pointers to manage memory lifetimes, this is your first guess.

What absolute rubbish. Ever heard of a constructor/destructor (aka RAII)? You
can go really far without the usage of any new/delete _or_ a smart pointer,
and this probably should be the case all the way up till the OS boundary.

~~~
golergka
Most of the time, RAII is a good idea. Not always, though.

[https://www.google.ru/url?sa=t&source=web&rct=j&url=%23&ved=...](https://www.google.ru/url?sa=t&source=web&rct=j&url=%23&ved=0ahUKEwi5js_0iuTaAhVC4YMKHWv7C5AQxa8BCCUwAA&usg=AOvVaw0VTJzWxrN8ZFOD4xbU2nov)

(Sorry for weird url, YouTube seems to be currently blocked here and I'm too
lazy to wait for a VPN connection).

~~~
chrismorgan
That URL doesn’t work at all—just redirects to #.

~~~
golergka
It was to Jonathan's Blow presentation for ideas about a language for game
programming, where he goes in depth about RAII, C/C++, Go, Rust and other
modern language developments.

------
dmccrevan
What type was the GNOME terminal leak?

~~~
dingo_bat
I think that corresponds to the 2nd type mentioned in the article.

