简体   繁体   中英

How do I figure out how much memory by ehcache is using?

I'm using JBoss 7.1.3.AS with Java 6 and this version of ehcache

            <dependency>
                    <groupId>org.hibernate</groupId>
                    <artifactId>hibernate-ehcache</artifactId>
                    <version>5.1.0.Final</version>
            </dependency>

We are running on Amazon Linux. How do I determine how much memory my ehcache is using? I can see statistics about how much JBOss is using with top …

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND   
12159 jboss     20   0 14.9g 5.7g  25m S 163.2 19.3 225:18.15 java

and how much free memory is on our system …

[dalvarado@east1c ~]$ free -m
             total       used       free     shared    buffers     cached
Mem:         30104       8099      22004          0        161       1859
-/+ buffers/cache:       6078      24026
Swap:            0          0          0

but I'm interested in figuring out how much memory our ehcache is using specifically so that we may resize the number of entries in our default cache. Note that because we are in a production environment, adding new Java code is not an immediate option. Below is the ehcache configuration …

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd" updateCheck="false">

<!-- This is a default configuration for 256Mb of cached data using the JVM's heap, but it must be adjusted
     according to specific requirement and heap sizes -->
<defaultCache maxElementsInMemory="200000"
     eternal="false"
     timeToIdleSeconds="86400"
     timeToLiveSeconds="86400"
     overflowToDisk="false"
     memoryStoreEvictionPolicy="LRU">
</defaultCache> 
<cache name="main" maxElementsInMemory="200000" />

 <cacheManagerPeerProviderFactory
     class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
     properties="peerDiscovery=automatic, multicastGroupAddress=230.0.0.1,
     multicastGroupPort=4446, timeToLive=32"/>

<cacheManagerPeerListenerFactory
    class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
    properties="hostName=localhost, port=40001,
    socketTimeoutMillis=2000"/>    

</ehcache>

If you are allowed to take a heap dump of your running java app, take it and analyze with memory analyzer, for example with MAT http://www.eclipse.org/mat/

For taking heap dumps, you can use jmap . Identify the pid of your java process, for example with

ps aux |grep java

and then

jmap -dump:format=b,file=heap_dump.hprof <pid>

Open the file heap_dump.hprof with MAT.

Memory Analyzer provides a dominator tree of the object graph, so you can select ehcache cache objects as roots and see child cached objects in details.

EhCache provides net.sf.ehcache.management.sampled.Sampled Cache for this. Register it with JMX. It exposes, among other things, the heap in bytes being used by a registered cache.

Creating it is easy enough, it just wraps an existing cache:

SampledCache sampledCache = new SampledCache(cache);

The amount of data it exposes is quite useful: API

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM