dev@grizzly.java.net

Grizzly 2.0: monitoring memory consumption

From: Oleksiy Stashok <Oleksiy.Stashok_at_Sun.COM>
Date: Fri, 19 Mar 2010 18:45:10 +0100

Hi,

I've implemented simple interface MemoryManagerMonitoringProbe, using
which it's possible to get statistics on Grizzly memory consumption.
For example:

MyMemoryMonitoringProbe probe = new MyMemoryMonitoringProbe();
DefaultMemoryManager mm = new DefaultMemoryManager(probe);

TransportFactory.getInstance().setDefaultMemoryManager(mm);

// Start Grizzly
....................

// Log the memory monitoring probe statistic
log(probe.toString());

//////////// Simple monitoring probe implementation

     public static class MyMemoryMonitoringProbe implements
MemoryManagerMonitoringProbe {
         private final AtomicLong allocatedNew = new AtomicLong();
         private final AtomicLong allocatedFromPool = new AtomicLong();
         private final AtomicLong releasedToPool = new AtomicLong();

         public void allocateNewBufferEvent(int i) {
             allocatedNew.addAndGet(i);
         }

         public void allocateBufferFromPoolEvent(int i) {
             allocatedFromPool.addAndGet(i);
         }

         public void releaseBufferToPoolEvent(int i) {
             releasedToPool.addAndGet(i);
         }

         @Override
         public String toString() {
             StringBuilder sb = new StringBuilder();
             sb.append("allocated-memory=").append(allocatedNew.get());
             sb.append(" allocated-from-
pool=").append(allocatedFromPool.get());
             sb.append(" released-to-
pool=").append(releasedToPool.get());

             return sb.toString();
         }
     }