×
Namespaces

Variants
Actions

Talk:Reduce JAR Size

From Nokia Developer Wiki
Jump to: navigation, search

There are some good tips , but a lot of mistakes in this artical: Using byte instead of integer does not reduce the jar size, rather it reduces the heap size. Changing "zip settings" does not reduce jar in anyway Try different visibility modifiers of members and methods - really need to clarify that advise... what modifier changes exactly make jar smaller? "If it is possible try to combine all variables or class members of the same type in the array" again this is a heap reduction, not jar size


Note that some actions only sometimes reduce jar-size!


Example:
Let:
public class InlineMe {
private boolean b = false;
private int ints[] = null;
public boolean isB() {
return b;
}
public void setB(boolean b) {
this.b = b;
}
protected int[] init(int r, int len){
ints = new int[len];
for (int i = 0; i < ints.length; i++) {
ints[i] = r;
}
return ints;
}
public void print(){
for (int i = 0; i < ints.length; i++) {
int anInt = ints[i];
System.out.print("anInt = " + anInt+" ");
}
System.out.println("");
}
}
The changed class is such:
public class Main {
private static final int LEN = 6;
// public int[] ints;
int i1 = 5;
int i2 = 5;
int i3 = 5;
int i4;
 
public static void main(String[] args) {
InlineMe ims[] = new InlineMe[LEN];
 
Main m = new Main();
m.i4 = 5;
/*m.ints = new int[4];
for (int i = 0; i < m.ints.length; i++) {
m.ints[i] =5;
}*/

 
for (int i = 0; i < ims.length; i++) {
ims[i] = new InlineMe();
InlineMe im = ims[i];
im.init(i * 2, i + 1);
im.setB(i % 2 == 0);
}
for (int i = 0; i < ims.length; i++) {
InlineMe im = ims[i];
if (im.isB())
im.print();
}
}
}
It is 1895 bytes without obfuscating.
And this is how I change it (i's variables are in array):
public class Main {
private static final int LEN = 6;
public int[] ints;
// int i1 = 5;
// int i2 = 5;
// int i3 = 5;
// int i4;
 
public static void main(String[] args) {
InlineMe ims[] = new InlineMe[LEN];
 
Main m = new Main();
// m.i4 = 5;
m.ints = new int[4];
for (int i = 0; i < m.ints.length; i++) {
m.ints[i] =5;
}
 
for (int i = 0; i < ims.length; i++) {
ims[i] = new InlineMe();
InlineMe im = ims[i];
im.init(i * 2, i + 1);
im.setB(i % 2 == 0);
}
for (int i = 0; i < ims.length; i++) {
InlineMe im = ims[i];
if (im.isB())
im.print();
}
}
}
Now 1888 bytes... sometimes:)

atiskov 16:07, 24 May 2007 (UTC)

Well, the byte code of an array element is more than the byte code of a normal integer reference, so it's just a matter of referencing the array elements enough times and the array version of the code will be bigger. But this is really an acedemic argument. If you try to write code this way, micro-managing it to minimise the code size, your code is going to end up totally unmanageable and your never going to finish your application on time.


"your code is going to end up totally unmanageable" - I agree :) --atiskov 10:14, 28 May 2007 (UTC)


obfuscator and inlining

Most of the obfuscators perform methods inlining, on a method length criterion.

Each case is different, so play around with the inlining limit

 

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×