JesusFreke / smali

smali/baksmali
6.3k stars 1.07k forks source link

How to calculate the contribution of each class to the dex #871

Open hailuo966 opened 1 year ago

hailuo966 commented 1 year ago

Can I use baksmali.jar to calculate the contribution of each class to the dex file size? Or how can I calculate the contribution of each class to the dex file size? Thanks a lot.

CunningLogic commented 1 year ago

Complex question, the actual size will vary, certain data can be shared between classes.

For instance, strings, two classes may have the same strings within, but you only get on entry into the string index table.

and some compilers use space saving tricks, such as if two methods have identical dubug info items, they with only write one and and set the debug_info_off to the same offset.

With such data sharing, how do you calculate the size contribution?

Lanchon commented 1 year ago

java class files have a lot of redundancy, especially when they cross-link (eg: when they are part of the same program). java class files are heavily processed during class loading and the VMs are expected to have a very different and more compact representation in memory after loading, with much of this redundancy reduced.

dex files were designed for low memory devices and structured in such a way that they are ready for VM interpretation straight out of the file contents. they can simply be memory mapped and interpreted from there. so much of the java class loader processing needed to load java class files into memory is already done at dex build time when the dex file is constructed. redundancy is reduced and data is shared among classes.

so if A and B are classes, all you can (mostly) say is: size(dex({A})) + size(dex({B})) >= size(dex({A, B})) (in reality through, the use of larger indexes in bigger dex files might make that equation not hold.)