Data Sizer Java Utility Class

    The attached Java class is an example utility that includes methods for determining in-memory size of individual domain objects, and entries in GemFire regions in serialized and deserialized form. It uses GemFire's own internal sizing facilities, and so it has a dependency on gemfire.jar. Please note that this is an example. It has not been thoroughly tested and it is not intended for production use.


    Usage examples


    Calculating the size of a pure domain object


    The following code sample shows how to size and get an object histogram for an instance of Portfolio class:


        Object obj = new Portfolio();

        int objSize = SizeCalculator.sizeObject(obj);
        // Get object histogram
        String objHist = SizeCalculator.histObject(obj);



    Printing the string objHist will produce output similar to the following:


    clazz                               size    count
      class CollectionHolder              96     4
      class java.util.HashMap            128     2
      class Portfolio                    152     1
      class java.util.HashMap$Entry      288     6
      class [Ljava.lang.String;          472     5
      class Position                     608     4
      class [Ljava.util.HashMap$Entry;   144     2
      class java.lang.String            2976    62
      class [B                          1208     1
      class [C                          1760    62



    Calculating the size of data in a GemFire region


    Given a Gemfire region, the sizer can be invoked on it in a couple of ways, as shown in the following code snippet:



        Region dataRegion = ... // get a reference to the data region

        // Create an instance of SizeCalculator that prints to System.out

        SizeCalculator sizer = new SizeCalculator();

        // Iterate over all the objects in the region, size it, and

        // report the total and average size


        // Iterate over 10 objects in the region, and report the total

        // and average size

        sizer.sizeRegion(dataRegion, 10);



    The output from SizeCalculator.sizeRegion will look like so:


    Total RegionEntry size (serialized): 28481408
    Total RegionEntry size (deserialized): 61559808
    Total Key size: 240000
    Total Value size (serialized): 27206362
    Total Value size (deserialized): 60519808
    Average RegionEntry size (serialized): 2848
    Average RegionEntry size (deserialized): 6155
    Average Key size: 24
    Average Value size (serialized): 2720
    Average Value size (deserialized): 6051