Torsten Curdt’s weblog

Goodbye 2008, Welcome 2009

Last year was exciting – full of change and I see even more coming for 2009. So what was 2008 for me? For one I finally “got” twitter and embrace it since. I noticed it means fewer but hopefully also more meaningful blogs posts. Still I’ve got considerably more readers this year. Thanks for listening everyone! The most popular post of 2008 were:

  1. Opening UIF files on OSX
  2. From subversion to git (part3)
  3. From subversion to git (part1)
  4. Debugging HTTPS
  5. Suunto Gekko with OSX
  6. From subversion to git (part2)
  7. Where is this java class from?
  8. Sending emails from Cocoa
  9. Battery Statistics
  10. Java classpath and directories

Read the rest of this entry »

Where is this java class from?

Recently James blogged about a nice addition to log4j. The submission even got applied already.

It uses the same “trick” that I had stashed away in my “toblog” folder and almost had forgotten about. Finding out the what jar file a class comes from can be quite useful at times. The straight forward approach is to lookup the resource and go from there.

Class type = yourinstance.getClass();
File jar = new File(type.getClassLoader().getResource(
  type.getName().replace('.', '/') + ".class")
  .replaceFirst("^[^/]*", "")
  .replaceFirst("!.*", ""));

But a much simpler and faster approach is to use the ProtectionDomain (if available).

Class type = yourinstance.getClass();
File jar = new File(type.getProtectionDomain()

Really quite simple but useful.

Java classpath and directories

The java classpath and directories has always been quite a sad story. Of course you were always able to pass a directory of classes to the jvm like this:

java -classpath classes Main

But what you usually deal with these days are jars. Often quite a bunch of them. So fair enough – let’s add a directory with jars as well:

java -classpath classes:lib Main

Up until java 5 all you got was a ClassNotFoundException because java did not search for jars but classes in there. It just ignored the jars. So what pretty much everyone ended up doing is providing yet another shell script to build up the classpath and pass it to the jvm via command line. Of course for bigger projects this let to…

java -classpath lib/commons-logging-1.1.1.jar:lib/commons-jci-core-1.0.jar:commons-io-1.2.jar:lib/junit-3.8.2.jar:lib/maven-project-2.0.8.jar:lib/rhino-js-1.6.5.jar: ...

I guess you see where I am going …an unmanageable mess of a command line. But thankfully times have changed. Starting with java 6 (mustang) you can now use wildcards in the classpath:

java -classpath classes:lib/'*' Main

Naturally this means that the order of the jars is implicit. Which in turn means you need to be extra careful when there are classpath clashes. But in general I think this is a nice feature that should have been there from day one …and I just found out about it.

Perceived Performance

beforeWhile tuning your server for performance is a good thing it’s not the only thing you should look at. Lately my blog pages felt to be loading a bit slow. Looking at the Safari network timeline it’s obvious that the flickr badge is (at least sometimes) dog slow and prevents the page to build up. The perceived performance suffers. And this has nothing to do with my server at all.

As there really no need for the flickr badge to be up-to-date to the second. I am caching it locally now. A cron script gets a copy and writes it to disk.

$ cat /etc/cron.daily/flickr
curl -s --max-time 120 --connect-timeout 60 \
 'http://www.flickr.com/badge_code_v2.gne?count=3&display=latest&size=s&layout=h&source=user&user=88457640%40N00' \
 | grep -v "flickr_badge_beacon" \
 > /path/to/cache/location/flickr.html

Then then it gets included like this

<script type="text/javascript">
<?php readfile("/path/to/cache/location/flickr.html"); ?>

As a result the load time decreased from 6.3s down to 1.6s. That’s almost 4x faster. At least in the user’s perception. YSlow is also a great plugin for firefox that helps exactly with this kind of optimizations.

Would probably be a good idea to turn this into a wordpress plugin.

Memory on the iPhone

iPhone MemoryNow here is the question: How much memory does the iPhone have? How much memory is OK for applications to use? Curious as I am I did some research and wrote a little test project. This got me some odd results.

According to what you find on the net the iPhone has a total memory size of 128 MB and your application should not use more than 46MB of it. But what happens exactly when you try to obtain more?

So I wrote a little application that continuously allocates more and more memory. In fact it mallocs/frees the memory on every timer tick.

Now this is where it gets odd. While the sysctl() calls do report something around 128MB of RAM I can malloc() way beyond this. In fact calling malloc(700000000) does not fail at all! When I run the application on just the iPhone it will stop at around 719MB. When I run it through Instruments the whole devices freezes at around 46MB. This has been reproduced on 2.1 and 2.2 on different devices.

- (void)tick
    allocated = allocated + size;

    if (allocatedPtr) {

    allocatedPtr = malloc(allocated);

    if (!allocatedPtr) {
        NSLog(@"out of memory at %ld", allocated);

Later I found someone who stumbled across the same thing.

Lazyweb: what is going on here?

You can download the test application here. But as a disclaimer: you run this code on your own risk!


So afterall this means the result of malloc() has a different semantic of what I expected. Adding the following piece of code makes the program behave and gives the expected result.

    long *p = (long*)allocatedPtr;
    long count = allocated / sizeof(long);
    long i;
    for (i=0;i<count;i++) {
        *p++ = 0x12345678;

So it turns out if you allocate (and use!) around 46-50 MB in your iPhone application it will just get terminated.