Use and Abuse of Garbage Collected Languages
Programming in the 21st Century - James Hague - April 21, 2012The garbage collection vs. manual memory management debates ended years ago. As with the high-level vs. assembly language debates which came before them, it’s hard to argue in favor of tedious bookkeeping when there’s an automatic solution. Now we use Python, Ruby, Java, Javascript, Erlang, and C#, and enjoy the productivity benefits of not having to formally request and release blocks of bytes.
But there’s a slight, gentle nagging—not even a true worry—about this automatic memory handling layer: what if when my toy project grows to tens or hundreds of megabytes of data, it’s no longer invisible? What if, despite the real-time-ness and concurrent-ness of the garbage collector, there’s a 100 millisecond pause in the middle of my real-time application? What if there’s a hitch in my sixty frames per second video game? What if that hitch lasts two full seconds? The real question here is “If this happens, then what can I possibly do about it?”
These concerns aren’t theoretical. There are periodic reports from people for whom the garbage collector has switched from being a friendly convenience to the enemy. Maybe it’s because of a super-sized heap? Or maybe accidentally triggering worst-case behavior in the GC? Or maybe it’s simply using an environment where GC pauses didn’t matter until recently?
Writing a concurrent garbage collector to handle gigabytes is a difficult engineering feat, but any student project GC will tear through a 100K heap fast enough to be worthy of a “soft real-time” label. While it should be obvious that keeping data sizes down is the first step in reducing garbage collection issues, it’s something I haven’t seen much focus on. In image processing code written in Erlang, I’ve used the atom transparent to represent pixels where the alpha value is zero (instead of a full tuple: {0,0,0,0}). Even better is to work with runs of transparent pixels (such as {transparent, Length}). Data-size optimization in dynamic languages is the new cycle counting.
There’s a more often recommended approach to solving garbage collection pauses, and while I don’t want to flat-out say it’s wrong, it should at least be viewed with suspicion. The theory is that more memory allocations means the garbage collector runs more frequently, therefore the goal is to reduce the number of allocations. So far, so good. The key technique is to preallocate pools of objects and reuse them instead of continually requesting memory from and returning it to the system.
Think about that for a minute. Manual memory management is too error prone, garbage collection abstracts that away, and now the solution to problems with garbage collection is to manually manage memory? This is like writing your own file buffering layer that sits on top of buffered file I/O routines. The whole point of GC is that you can say “Hey, I’d like a new [list/array/object],” and it’s quick, and it goes away when no longer referenced. Memory is a lightweight entity. Need to build up an intermediate list and then discard it? Easy! No worries!
If this isn’t the case, if memory allocations in a garbage collected language are still something to be calorie-counted, then maybe the memory management debates aren’t over.
(If you liked this, you might enjoy Why Garbage Collection Paranoia is Still (sometimes) Justified.)
Categories: Blogs Programming in the 21st Century
Erlang on Twitter
» lhoguin (Loïc Hoguin): RT @erlangfactory: Want to share some your ideas at the #EUC 2013 stage? You are welcome to submit you lightning talk here http://t.co/Pkyt…
» lhoguin (Loïc Hoguin): RT @erlangfactory: Registration for the #EUC tutorials is open! 12 June at #Ericsson in KIsta. Number of spots limited.Select tutorials htt…
» Eric_Rainey (Eric Rainey): New @pragprog book: “Programming Elixir: Functional |> Concurrent |> Pragmatic |> Fun” http://t.co/4mieCRc4lD #erlang #fp #programming
» akohli (akohli): Adtech building a real time bidding platform in dublin with Erlang #erlangfactory
» JuaryPerdana (juary perdana iqbal): Kasian dya…..!! @athenafuguach @erlang @SandyBani99 @ibnujauzi95 @adittyaHari @AyankRinie http://t.co/1GXEntEgMz
» akohli (akohli): Adtech describing their use of Erlang otp to serve ads - up to 150 billion a month. #erlangfactory
» atulatri (atul atri): Came across Success Typings (dialyzer) in #Erlang! Wonderful! Why don’t they implement same thing in #PHP
» akohli (akohli): Erlang’s good at integration #erlangfactory
» m0wfo (Chris Mowforth): RT @erlangfactory: Dublin #ErlangFactory Lite is on Thursday 23 May! If you haven’t booked your seat yet, do it now! Register for free http…
» kardan (Daniel Karlsson): Impressed by @elixirlang but find it a bit ironic that Prag Prog’s intro video start with pseudo Erlang and then adds Ruby def, :do & end
Statistics
Number of aggregated posts: 10651
Most recent article: May 21, 2013
Latest comments
» Moraru on This is Why You Spent All that Time Learning to Program: It is true that computer science was a pain in the back at time that i’ve had to learn it…
» Commercial hand dryers on Couchbase Meetup at new HQ: Buy online from here where you will get so much of variety in Commercial hand dryers for people. If you…
» Fort McMurray Homes on Motivated Reasoning and Erlang vs Python vs Node: I don’t really understand why this post is motivational? I don’t even see a post, just a title. Fort McMurray…