0
import java.util.ArrayList;
import java.util.List;

import net.sf.ehcache.pool.sizeof.UnsafeSizeOf;

public class EhCacheTest {

    private List<Double> testList = new ArrayList<Double>();

    public static void main(String[] args) {
        EhCacheTest test = new EhCacheTest();
        for(int i=0;i<1000;i++) {
            test.addItem(1.0);
            System.out.println(new UnsafeSizeOf().sizeOf(test));
        }
    }

    public void addItem(double a) {
        testList.add(a);
    }
}

I used UnsafeSizeOf to calculate the size of Object 'test'. Nomatter how many doubles I add into the list, the size of 'test' is always 16 bytes.

Because of this, the maxBytesLocalHeap paramater is useless for me.

Guangheng Xu
  • 149
  • 1
  • 10

1 Answers1

0

Given your code snippet, this is totally expected.

See the javadoc of the method SizeOf.sizeOf, it clearly states that it sizes the instance without navigating the object graph.

What you want to use is SizeOf.deepSizeOf which will navigate the object graph.

However this test is flawed, cause you risk comparing that specific SizeOf implementation against another one, as Ehcache uses multiple implementations, depending on what's available in a specific environment.

If you really want to confirm the byte sizing on heap works, you are better off filling a cache, finding out the size it reports through statistics and then do a heap dump to see how much memory is effectively used.

Louis Jacomet
  • 13,661
  • 2
  • 34
  • 43