My intention is to analyze the bytecode of a Java program and collect the data about the data structures that have been used. That data includes the initial capacity and how the particular data structure has grown throughout the runtime(growing rate, according to the growing policy of the Java data structures).
Can I assume something like capacity is proportional to the memory taken by that particular data structure instance?
For example;
I used RamUsageEstimator
from com.carrotsearch.sizeof.RamUsageEstimator
to get the memory size taken by the particular data structure instance.
List<Integer> intList = new ArrayList<Integer>(4);
for(int i = 0 ; i < 5 ; i++){
intList.add(i);
System.out.println("Size(byte) -> " + RamUsageEstimator.sizeOf(intList));
}
I ran this code with an ArrayList
of an initial size of 4
. I added 5
elements to the list using a loop. According to the growing policy of the Java ArrayList
, it should grow itself by 50%, which means after the 4rth element when I enter the 5th element new size would be 6
. I got the following results, Bytes -> 72, 88, 104, 120, 144
.
We can clearly see here that between the first 4 elements, the byte gap is 16, and eventually, at the 5th element, it has become 24. So, it clearly shows the growth and the rate right?
Is it possible to achieve my task this way?
Any answer would be great! Thank you!