At the current phase of the project I’m into the optimization. We have a CSSHelper class that has all the possible css values set in the map that is statically initialized. So on RAZR it takes 50 seconds to just load the class into the memory, absolutely unacceptable.
So poking around the web I found this great slides from JavaOne on optimization techniques: Effective J2ME.
Below is a small paragraph on static array initialization:
Static Array Initialization
• Problem
• Array initialization performed in static initializer
• This can cause significant code bloat and slow down class loading and initialization
• Impact
• Application startup, memory footprint
• Guideline
• Move initialization out of static initializer and do programmatic initialization or read data from I/O
Solution here is to create binary file that would be created at build time and packaged into the jar. Add init() method to the class to read the binary file from the jar as resource.
technorati tags: j2me razr optimization static array initialization
2 Comments
The problem was not to do with initialization of the CSSHelper as initializing a couple of 100 constants does not take 50 seconds not even on a mobile phone. However this issue did reveal an interesting problem when a class uses final static arrays which are supposed to be removed in the shrinking/obfuscating phase (this is only verified with proguard). E.g. if you have:
private static final VALUES = {”Value1″, “Value2″, “Value3″};
and you remove all reverences to VALUES then the proguard obfuscater will remove VALUES in the class, however if you decompile the class you will find the following code:
static {
String[] a = new String[3];
a[0] = “Value1″;
a[1] = “Value2″;
a[2] = “Value3″;
}
Which means that the class constant pool will have constants which is never used. This will impact the application in two ways:1) the jar size will be larger size the constants is included. 2) the constants will increase the VM’s heap size since they are interned and the loading time of the class will increase because of this (e.g. it will take time to intern the strings and initializing the array which will later be garbage collected which also takes time).
The solution is to either use a flag in the definition of the static array, like:
private static final VALUES = FLAGS.INCLUDE ? {”Value1″, “Value2″, “Value3″} : null;
or to make use the entire class is removed in the obfuscation step.
Hi Alexey,
Would like to connect with you. Have an initiative in the mobile space and looking for a technologists.
Can you send me your email/ phone?
Thanks
Gal
Post a Comment