<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Jboss on despatches</title><link>https://icle.es/tags/jboss/</link><description>Recent content in Jboss on despatches</description><generator>Hugo</generator><language>en</language><lastBuildDate>Wed, 18 Mar 2026 15:13:17 +0000</lastBuildDate><atom:link href="https://icle.es/tags/jboss/index.xml" rel="self" type="application/rss+xml"/><item><title>Java Object Size In Memory</title><link>https://icle.es/2011/04/25/java-object-size-in-memory/</link><pubDate>Mon, 25 Apr 2011 15:58:00 +0000</pubDate><guid>https://icle.es/2011/04/25/java-object-size-in-memory/</guid><description>&lt;p>Anyone who has worked with java in a high end application will be well aware of
the double edged sword that is java garbage collection. When it works - it is
awesome but when it doesn&amp;rsquo;t - it is an absolute nightmare. We work on a
ticketing system where it is imperative that the system is as near real-time as
possible. The biggest issue that we have found is the running of memory in the
JVM which causes a stop the world garbage collection. This then results in
cluster failures since an individual node is inaccessible for long enough that
it is kicked out of the cluster.&lt;/p>
&lt;p>There are various ways to combat this issue and the first instinct would be
suggest that there is a memory leak. After eliminating this as a possibility,
the next challenge was to identify where the memory was being taken up. This
took some time and effort and the hibernate second level cache was identified.
We were storing far too much in the second level cache.&lt;/p>
&lt;p>This is another double edged sword. The hibernate second level cache is
absolutely imperative to a high performance system. It does however, come with a
price. The cache needs to be managed carefully to ensure that balance between
performance and memory requirements.&lt;/p></description><content:encoded><![CDATA[<p>Anyone who has worked with java in a high end application will be well aware of
the double edged sword that is java garbage collection. When it works - it is
awesome but when it doesn&rsquo;t - it is an absolute nightmare. We work on a
ticketing system where it is imperative that the system is as near real-time as
possible. The biggest issue that we have found is the running of memory in the
JVM which causes a stop the world garbage collection. This then results in
cluster failures since an individual node is inaccessible for long enough that
it is kicked out of the cluster.</p>
<p>There are various ways to combat this issue and the first instinct would be
suggest that there is a memory leak. After eliminating this as a possibility,
the next challenge was to identify where the memory was being taken up. This
took some time and effort and the hibernate second level cache was identified.
We were storing far too much in the second level cache.</p>
<p>This is another double edged sword. The hibernate second level cache is
absolutely imperative to a high performance system. It does however, come with a
price. The cache needs to be managed carefully to ensure that balance between
performance and memory requirements.</p>
<p>To this end, it was important to be able to identify what was taking up all the
memory in the cache. Each object might only take a couple of hundred bytes, but
with our second level cache set to store hundreds of thousands of items, this
quickly takes up hundreds of megabytes. With the metadata of the cache, this
could easily hike it up near a gigabyte of memory usage. This gets substantially
worse with cache evictions and the adding of new items into the cache.</p>
<p>The correct way to resolve this is to identify specific object types that
&ldquo;overload&rdquo; the cache. i.e. items that have an large number of instances stored
in the cache. Identifying classes that store a large number of items is easy
enough - we just traverse the cache and count up the number of items. However,
there might be a class that stores a smaller number of items but take a sizeable
amount of memory. For this reason, it is important to understand the object
sizes in memory as well.</p>
<p>If you have ever tried to find a way to identify object sizes, you will know
that this is no easy task. You can calculate to some degree of accuracy the size
of an object based on the data it stores but this is a manual process.</p>
<p>The only real way to get this information is to use a java agent and use that to
calculate a more accurate memory usage. For this purpose, we used the
<a href="http://www.javamex.com/classmexer/" title="ClassMexer Java Profiling Agent">classmexer agent</a>
which requires a simple installation step of adding the following parameter to
java <code>-javaagent:classmexer.jar</code>. You can then figure out the memory utilisation
of an object by calling</p>
```java
MemoryUtil.deepMemoryUsageOf(objectInstance)
```
<p>You can also pass in a collection of objects:</p>
```java
MemoryUtil.deepMemoryUsageOfAll(objectInstanceCollection)
```
<p>This was the simple part.</p>
<p>Traversing the node structure of jboss cache and collating a collection
statistics with regards to the number of each type of object and its memory
utilisation was a little more interesting.</p>
<p>I will cover this separately</p>]]></content:encoded></item></channel></rss>