Garbage Collection: The Magic Number is 6

I read a fascinating article recently on why mobile apps are so slow – the article is well worth the peruse as to why JavaScript apps will never keep up to C/C++ (note this also affects most interpreted languages like Python, Ruby, Java, VB.net/C# etc).

But the main point I remembered is the magic number for garbage collection: Six times the used memory. Or to put it another way, as long as you keep your program’s memory usage under 1/6 of available memory, life is good (I’m referring to GC-managed memory of course).

But go under that – especially under 4x (or 1/4) – and it gets very bad. In fact, the function for it is exponential, and under 1/6 memory the curve starts to climb fast. In the cases he mentioned in his post, some tests took 10-15 times as long to run when GC was used versus regular memory management.

This isn’t surprising of course: I programmed the Commodore 64 which had string garbage collection, and you soon got a feel for how many strings you could handle. Too many, and the computer abruptly halted while it cleaned them up. Today, however, most programmers on desktops never hit a performance issue, since 1/6th the memory is a LOT of strings or other data. But on small devices that 1/6 comes up quickly…

Morale of the story: Enjoy your Java, but never forget your malloc() and new[] – and be sure you remember ‘6’ while you craft your next Java app…

Comments are closed.