Josef “Jeff” Sipek

Memory Leaks

Alright, I think I’ve had just about enough. Why does Amarok eat up 22% of my RAM (1GB) after 4 days of running (and playing music for maybe 18 hours of those 4 days)? Why does Firefox use up 33% of my RAM in 4 days?

Why is it that when I shut down the app, and restart it, the usage is 4–5 times less?

Amarok Firefox 2
before app restart 225 MB 338 MB
after app restart 58 MB 72 MB

The only reason I can think of is application being buggy, or having really crappy defaults.

Buggy Applications

Dear developers, believe it or not, when you allocate memory, you also have to free it when you are done with it. If you don’t, you are committing a crime against humanity known as a “memory leak”. This memory is unusable, and essentially becomes dead weight the process carries around. Since it is not used, the OS may swap it out, and before long, your swap file/partition becomes full of memory that has been leaked.

Contrary to popular belief, freeing memory is really simple.

For you C++ coders (yes, that includes you Amarok folks), you simply use the delete keyword followed by a pointer of what you want to free. For example,

delete some_pointer;

If you are using C, the free function is your friend. Just call it, and make the one argument you give it the pointer to what you want to free. For example,

free(some_pointer);

Now, if you are working on a larger project, there might be wrappers around the memory management (malloc/free, new/delete) functions, but whatever the “free this memory” function is called, USE IT.

I can almost hear all the managed languages fans yell: “Just use a language that does garbage collection, and you won’t have to worry about freeing memory.” Well, you are WRONG!

Garbage collectors maintain graphs of memory allocations, and whenever they notice that some piece of memory is unreachable, they mark it as garbage, and free it. Here’s my favorite example for causing leaks in a garbage collected language:

Suppose that you have implemented a class that works as a stack. You implemented it as a list of elements, and an index into the array to mark the top of the stack. Pushing an element is trivial, you just increment the index, and set the reference in the array to the object you want to store. Popping is really easy, you just decrement the index, and you’re done. Right? WRONG! Decrementing the index changes that one integer variable, but that reference in the array is still valid, and therefore the object is still reachable as far as the garbage collector is concerned. Sure, next time you push into that slot, the previous reference will get broken, and the previous allocation will get freed (assuming that there are no other references). But what if you never push that many elements back onto the stack? What if you experienced some high-load spike? You’ll have a large number of objects incorrectly referenced, tieing up memory, and quite possibly making the entire system slower.

How can you solve this? Pretty simple, just reset the reference to some “null” quantity. In Java, that means using the null literal. For example,

some_reference = null;

In Python, None is the proper keyword to use:

some_reference = None

The lesson is, free the memory you allocated when you are done using it.

Crappy Defaults

Many large applications (Firefox included), have many options you can set that affect its behavior. The default options should cover 95% or more of the users (or at least the greatest majority possible). Why such a high number? Well, suppose you settle for making 90% of your users happy out of the box…that means that 1 in 10 people that try your app will not be happy with the defaults. How many will bother checking if there even are knobs they can turn to make it work the way they want? Not all. Some will just try to install another open source app written by someone else that does pretty much the same thing. So, the default options should make as many people happy as possible.

How does this tie into a third of my RAM being used by Firefox? Simple, I do not know if there are any knobs that would “fix” the problem I am seeing. For all I know, someone decided that it was a great idea to be really aggressive about caching web page content in memory — something that’s fine if you have 16GB RAM, but guess what most people don’t.

Whatever it is (defaults that don’t make sense or memory leaks), Firefox and Amarok have problems that must get addressed. What is one of the reasons people complain about Microsoft Word? It takes up tons of memory. Well, I don’t feel like throwing over 200 MB of RAM at an application that plays MP3s, displays a playlist, and cover art.

And before someone suggests that I use Firefox 3… I realize that it is all super-duper-better-than-ever, but let’s think for a second. When the original Firefox was released, it was hailed as the non-leaky, light-weight Mozilla. Then, things started to get slow again. Firefox 2 was supposed to be the super-fast, non-leaky browser. What happened? What happened to my >300 MB of RAM? Now, Firefox 3 is all the rage…do you see the pattern yet?

I think this brings up a larger issue. It’s no secret that I do some Linux kernel coding from time to time. In the kernel, there are leaks at times, but it seems that the kernel leaks are effectively non-existent compared to applications like Firefox. Don’t believe me? How come you can have a server run for over a year and it responds just as well after the year as it did when you booted it? Imagine running Firefox for a year without restarting it? Can you even imagine that? The Linux kernel doesn’t seem to be the only “non-leaky” (there are leaks, but they are very rare, and probably mostly in the ugliest parts of the kernel — device drivers), Apache performs quite well even after running for a while, PostgreSQL, and the list goes on and on.

Why is it that Firefox and other projects seem to have so many problems? The only thing I can think of is the quality control that goes into checking new code before it’s committed. In the kernel community, a patch may get rewritten a dozen times, submitted to mailing lists for review, get comments from people familiar with the subsystem, but also from other developers (and budding developers trying to understand the existing code). It takes a lot of effort to get a piece of code into the kernel, but in the end, that code is well written, well reviewed, and it should benefit most users. Do the Firefox, et. al., communities do this? I do not know, but somehow, I suspect that it isn’t the case.

mbox vs. maildir

Over the past two weeks, I decided to try converting some of the mboxes I have for mailing lists to maildirs. Last time I tried to do it, I noticed an unacceptable delay when I started mutt. Not when I tried to load the largest maildir. That made me give up.

For fun (or was it profit? ;) ), I decided to try again. And again, I saw that delay. This time around, I ran strace on mutt, and I found out that my custom .muttrc was making mutt believe that every file in the maildir was an mbox. Fixing up my .muttrc made the delay go away. Now, I have start up times that are the same as with purely mbox setup, but opening up these maildirs takes up WAY less time. I’m talking fraction of second instead of 5-10 seconds.

I’m considering converting all but the spam box to maildir. I really don’t need thousands of extra inodes which I’ll never use anyway, but at the same time, I don’t mind having one gigantic file full of the spam messages (I keep them because I don’t manually check that no good messages got misclassified by spamassassin). I am going to probably tell XFS to reserve some diskspace for it, to prevent lots of fragmentation from the constant open-write-close syscall cycle.

No Gas

As I have mentioned to many of you, this was my first week of interning at IBM (this is my second year). Well, on Monday, after I got out of work, I realized I needed to get gas if I wanted to get home. I stopped at the first gas station (just down the road), and there were signs on all the pumps saying “No Gas”. Strange, I thought to myself, and so I drove to the next one (about 100m), and guess what…that one also had “No Gas” signs. Somewhat puzzled, and a bit worried (what if all of Wikipedia article: Westchester was out of gas?!). Lucky me, the 3rd station which was mere 50m further down the road, had gas…$3.64 for a US gallon of regular (compared to about $3.40 at home — yes, I realize, that’s still nothing compared to what the folks in Europe are paying).

ICON XXVII

It is that time of year again…it is time for ICON! Well, it was time for ICON as it is over already. I took a fair amount of photos, to sort of document the 2.5 days - to make people want to go next year.

Friday

Friday is always a half-day, unfortunately, this year, I didn’t find many must-see panels (the Saturday is always the best anyway), so I used the time to pick up my badge and to spend some quality time with my friends. Hrm, looking at the schedule again, I see I missed a few things that would have been interesting to go to…oh well.

Saturday

As I just said, the Saturday at ICON is the best. The moment I got on campus, I could just look in a random direction and see something worth photographing. So, no to bore you too much with meaningless rambling, here are some photos…

This was the first photo I took, unfortunately it is blurry because I didn’t realize that I was still on manual focus :-/

Chicken, et.  al.

About 30 seconds later, I ran into this guy…he’s at ICON every year, and every time he has a “mobile” of some sort…This time around he had the bottom part of a Dalek from Dr. Who.

*mobile

About 30 seconds later (I told you, random direction & take photo would have worked):

no clue

Ah, and then I saw these guys getting ready to beat the crap out of each other:

Let’s get medieval

While resizing the photo to fit onto this page nicely (the original 6.1 megapixel image would be a bit too wide for most screens, not to mention the amount of data that’d have to be transfered!), I noticed that these knights were wearing:

A shoe is a shoe

If there’s something strange in your neighborhood, who you gonna call? My suggestions would be to call the owners of the following vehicle (they come every year, as far as I know):

Who you gonna call?

Parked right next to it, you had (unfortunately, there was no flux capacitor):

88 miles per hour!

The driver looked kind of alien, not to mention a bit green…

Yoda

Throughout the con, you had plenty of armed forces of all types roaming around:

no comment

And of course, if you have those guys, you got to have…dramatic, symphonic music indicating a bad guy is about to make an appearance

a fan + Darth Vader

Then I went to wait for an autograph of Jeffrey Combs, who plays a bunch of characters on Star Trek. After a rather short wait on a line, I got an autograph on a 8”x10” photo of Wikipedia article: Weyoun, and Jeffery Combs wrote on it: Jeff… Long Live the Dominion. <signature>Weyoun</signature>. Pretty cool, if you ask me :) In addition to the autograph, I also got to take a photo with him…

Jeff and Jeff

Costumed folks…

Master Shake

More people, in front of the ticketing booth:

The ticketing booth

Then, going back outside, I got two photos of dragons. The first one is a bit overexposed (the flash fired) and the second is much better exposure-wise. Somehow, I like how the first one turned out anyway…

Dragons

Dragon

Then, I was off to the Q&A session with Jeffrey Combs. I took quite a few photos, most of them were ok, but not great…I guess I need a better lens :-/

Q&A session with Jeffrey Combs

Right after the Q&A session, there was another session, with Tim Russ, who played a few characters on Star Trek. I got a photo of the two talking for about a minute:

Combs and Russ

Tim Russ’ Q&A session was fun, he made fun of William Shatner for overusing, and over-extending dramatic pauses, he even read a silly script idea.

Tim Russ

After the Q&A session, Tim Russ did an autograph session. I decided that I didn’t have enough time because of a panel I was supposed to go to (and he was going to have another on Sunday anyway), and so I just quickly ran up to the indoor track, and took a bunch of photos with the intention to make a panorama:

Dealer room

If you want to see the full-resolution image, no problem, just beware that it is almost 4MB jpeg file (6561 by 3336 pixels).

And back to people shots…

no clue

I don’t remember seeing anyone dressed as Pikachu, so here’s a Pikachu toy thing:

Pika!

A dude dressed as a wolf thing of some sort, a dude (the bloody looking one) with a chainsaw for his right hand (unfortunately blocked by the wolf creature) dressed up as Ash from Evil Dead/Army of Darkness, and someone as Temari from Naturo:

A trio

I dare not say his name! (Plus a Luigi in the background.)

Beatlejuice!

No idea what kind of deeper meaning there might be, so I’ll state the obvious… It is a wolf/fox of some sort.

no idea

Seeing this amused me greatly:

again, no idea

And last photo for the day, a Mario & Luigi (different from the one above) combo:

Mario + Luigi

Then it got dark, and I got more busy attending panels so that’s it photo wise.

Sunday

I took only a few photos for the entire day. I didn’t feel like taking more photos of the people I already got on Saturday, and overall, the costumes aren’t as numerous on the last day.

Here’s some conference goer getting a photo with a (real, from the movie) Ghostbuster (Ernie Hudson, if I remember correctly):

Ernie Hudson (left)

A conference goer dressed up in SG-1 base uniform: Conference-goer

LIARS (a somewhat unfortunate acronym of the Long Island Advanced Rocketry Society):

LIARS

I messed up the exposure for the few photos (these are the best 2) of the medieval folks:

Ouch

Ouch, again

And that’s it photo wise. I did get my Tim Russ autograph; I went to some more panels, virualy all of which were enjoyable (the panel about taking over the world got canceled :( ).

Randomness

After ICON ended, a bunch of us went to get dinner. After eating, and talking about politics at a very interesting level (not about the upcoming election, but about things like FISA, etc.), a friend of mine and I decided to go outside and take a bunch of photos of a building and try to make a high dynamic range photo.

The taking photos part was pretty simple, it took about 20 minutes to take the five good photos (1, 2, 4, 8, 15, and 30 second exposures).

Then we went back to the lab, and loaded them up in qtpfsgui. This program does two things…

  • Combines the photos into one with higher dynamic range
  • Optionally, Wikipedia article: tone maps

The building we picked doesn’t really lend itself all that much to high dynamic range photography - well, it’s somewhat hard to spot the few places where the higher dynamic range helps. We tried the tone mapping, and the best two algorithms were Reinhard and Fattal.

First, Reinhard. It has a pretty natural look (at least compared to what Fattal produced), but at the same time, it feels…unreal.

Reinhard algo

We actually ran Fattal twice. Once with and once without noise reduction. The one without looks quite artsy, at least in my opinion.

Fattal algo

And the one with noise reduction looks a lot like it was drawn with colored pencils:

Fattal algo with noise reduction

I love the outcomes, and I’m pretty sure I’ll try to make more tone mapped images.

Linux is Hell

I think I’ve have just about enough of Linux…it barely works. It’s the same story over and over. You want to do something productive, and then you find out that you can’t for a million different reasons — ranging from “trivially solvable” upgrade some library (of course that’ll mean that half a dozen other libraries will want to get upgraded, and along with them half the system), to more serious - the .doc you got won’t open with OpenOffice because it’s using the latest and greatest MS Office format.

So, with all that said, I decided to reinstall my system….here’s what my desktop looks like now:

Linux is link hell, Inferno is like heaven

I couldn’t be happier.

Edit: This was of course April fools’ joke…I’m still using Linux.

Powered by blahgd