Hi,

In the meantime I dug deeper down in the mailing archives and I got the
impression that I will need to use the JCSAdmin.jsp page - backed by
JCSAdminBean class - to get size information (both in terms of number of
objects and number of bytes).

However when I tried to use it this functionality did not work
correctly. I used it to monitor the remote cache server and the byte
size just returned the number of objects. I looked into the source code
and discovered that in getByteCount() method:

public int getByteCount( CompositeCache cache )
        throws Exception
    {
        MemoryCache memCache = cache.getMemoryCache();

        Iterator iter = memCache.getIterator();

        CountingOnlyOutputStream counter = new
CountingOnlyOutputStream();
        ObjectOutputStream out = new ObjectOutputStream( counter );

        // non serializable objects will cause problems here
        // stop at the first non serializable exception.
        try
        {
            while ( iter.hasNext() )
            {
                ICacheElement ce = (ICacheElement) ( (Map.Entry)
iter.next() ).getValue();

                out.writeObject( ce.getVal() );
            }
        }
        catch ( Exception e )
        {
            log.info( "Problem getting byte count.  Likley cause is a
non serilizable object." + e.getMessage() );
        }

        // 4 bytes lost for the serialization header

        return counter.getCount() - 4;
    }

The ce.getVal() returns null for those objects which are instanceof
CacheElementSerialized (as opposed to those ones that are instanceof
CacheElement). The code mentions that getVal() is included in that class
only for backward compatibility only.

    /**
     * Backward compatibility.
     */
    public Serializable getVal()
    {
        return null;
    }

I checked that the wrapper for my cached objects were instanceof
CacheElementSerialized so in order to get the real size of the objects
JCSAdmin should have called getSerializedValue():

    /*
     * (non-Javadoc)
     * @see
org.apache.jcs.engine.behavior.ICacheElementSerialized#getSerializedValu
e()
     */
    public byte[] getSerializedValue()
    {
        return this.serializedValue;
    }

I used this method in my code and it seems to be working all right.

When I checked the length of the byte[] in a CacheElementSerialized
wrapper representing an Integer object I got 81 bytes. 

- Does this seem reasonable or it still contains an overhead?

- Also I don't know what does the comment mean about subtracting 4 bytes
of serialization header. Could anyone explain?

Thanks in advance,
Zsolt



-----Original Message-----
From: Thomas Vandahl [mailto:[EMAIL PROTECTED] 
Sent: 22 March 2007 18:45
To: JCS Users List
Subject: Re: size of memory cache in bytes

Zsolt Varszegi wrote:
> I would like to know if it is possible to query the size of the memory

> cache in bytes (as opposed to number of objects)?

I could use a memory cache that can be limited in memory size rather
than number of objects. Any idea how to do this?

Bye, Thomas.

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to