Monthly Archives: July 2013

Hiccups with Java double brace initialization of anonymous object

Double brace initialization is often used technique to overcome Java limitations on working with anonymous objects. In my case I used HashMap to pass tracking event parameters to Flurry Analytics class. The code is like this :

final HashMap<String, String> eventParms = new HashMap<String, String>() {{
    put("fileSize", Long.toString(new File(item.path).length()));
    put("mimeType", mimeType);
}};
FlurryAgent.logEvent(event , eventParms);

Looks pretty nice, and avoids creating local variable and then putting parameter pairs into it. However I started seeing OutOfMemoryExceptions in my Android application. I took HPROF memory dump, and pretty quickly realized that the problem was with anonymous classes they somehow were of a huge size. I googled around and found this article : http://vanillajava.blogspot.ru/2011/06/java-secret-double-brace-initialization.html, and it became obvious that it was exactly my problem: Anonymous object retained reference to outer class, which in my case had members of a big size – Bitmaps etc. So while Flurry events were kept int the queue those huge bitmaps ere also occupying heap memory.

Additional note on HPROF: In order to use standard Memory Analyzer tool you need to convert HPROF heap dump from Android format to regular Java format. Use android_sdk/tools/hprof-conv.

HTTP benchmark suites

Brief reference on HTTP benchmark I know

  • ab – Apache Benchmark, fairy simple
  • tsung – written in Erlang, supports multiple worker nodes
  • JMeter – witten in Java, supports multiple worker roles
  • Yandex-Tank – written in Python. Good documentation how to configure Linux kernel – https://github.com/yandex-load/yandex-tank
  • Loader.io – clous bases service

Note: I still to find my ideal tool. Creating a calibrated high load is not a trivial task. Simple hand-written load generator on Node.js provided 10%-20% better performance than ab. OS also plays a role here. I found it to be very hard to generate stable outgoing load from Linux machine. Occasionally outgoing TCP connection handshake would freeze for multiple of 3s intervals (tcp retry), or kernel will run out of ephemeral ports, as thousands of sockets are in FIN_WAIT state. I’m speaking about big RPS here, like > 1000 rps per core.