Calculating the Size (in Bytes and MB) of a Oracle Coherence Cache

The concept and usage of data grids are becoming very popular in this days since this type of technology are evolving very fast with some cool lead products like Oracle Coherence. Once for a while, developers need an programmatic way to calculate the total size of a specific cache that are residing in the data grid. In this post, I will show how to accomplish this using Oracle Coherence API. This example has been tested with 3.6, 3.7 and 3.7.1 versions of Oracle Coherence.

To start the development of this example, you need to create a POJO ("Plain Old Java Object") that represents a data structure that will hold user data. This data structure will also create an internal fat so I call that should increase considerably the size of each instance in the heap memory. Create a Java class named "Person" as shown in the listing below.

Now let's create a Java program that will start a data grid into Coherence and will create a cache named "People", that will hold people instances with sequential integer keys. Each person created in this program will trigger the execution of a custom constructor created in the People class that instantiates an internal fat (the random amount of data generated to increase the size of the object) for each person. Create a Java class named "CreatePeopleCacheAndPopulateWithData" as shown in the listing below.

package com.oracle.coherence.demo;importcom.oracle.coherence.domain.Person;importcom.tangosol.net.CacheFactory;importcom.tangosol.net.NamedCache;publicclassCreatePeopleCacheAndPopulateWithData{publicstaticvoidmain(String[] args){// Asks Coherence for a new cache named "People"...
NamedCache people = CacheFactory.getCache("People");// Creates three people that will be putted into the data grid. Each person// generates an internal fat that should increase its size in terms of bytes...
Person pessoa1 =new Person("Ricardo","Ferreira","ricardo.ferreira@example.com");
Person pessoa2 =new Person("Vitor","Ferreira","vitor.ferreira@example.com");
Person pessoa3 =new Person("Vivian","Ferreira","vivian.ferreira@example.com");// Insert three people at the data grid...
people.put(1, pessoa1);
people.put(2, pessoa2);
people.put(3, pessoa3);// Waits for 5 minutes until the user runs the Java program// that calculates the total size of the people cache...try{
System.out.println("---> Waiting for 5 minutes for the cache size calculation...");
Thread.sleep(300000);}catch(InterruptedException ie){
ie.printStackTrace();}}}

Finally, let's create a Java program that, using the Coherence API and JMX, will calculate the total size of each cache that the data grid is currently managing. The approach used in this example was retrieve every cache that the data grid are currently managing, but if you are interested on an specific cache, the same approach can be used, you should only filter witch cache will be looked for. Create a Java class named "CalculateTheSizeOfPeopleCache" as shown in the listing below.

I've commented the overall example so, I don't think that you should get into trouble to understand it. Basically we are dealing with JMX. The first thing to do is enable JMX support for the Coherence client (ie, an JVM that will only retrieve values from the data grid and will not integrate the cluster) application. This can be done very easily using the runtime "tangosol.coherence.management" system property. Consult the Coherence documentation for JMX to understand the possible values that could be applied. The program creates an in memory data structure that holds a custom class created called "Statistics".

This class represents the information that we are interested to see, which in this case are the size in bytes and in MB of the caches. An instance of this class is created for each cache that are currently managed by the data grid. Using JMX specific methods, we retrieve the information that are relevant for calculate the total size of the caches. To test this example, you should execute first the CreatePeopleCacheAndPopulateWithData.java program and after the CreatePeopleCacheAndPopulateWithData.java program. The results in the console should be something like this: