[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.6

Memory leak in Caching Object?

Asked by Bissyke in Java Programming Language, New to Java Programming

Tags: memory, leak

Hi, we're currently using a self made Caching object. The problem is our jserv proces is constantly running out of memory. We now think the only place where our memory leak can be situated is this Caching Object. Anyone who can see a bug wich can couse a memory leak?

Many thanks.

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.Arrays;
import java.util.Hashtable;
import java.util.Enumeration;

public class Caching
{

////////////////////////////////////////////////////////////////////////////////
//  Internal Fields
////////////////////////////////////////////////////////////////////////////////
    private static final Log gLog = LogFactory.getLog(Caching.class);

    private static final Hashtable gCaches = new Hashtable();

    private String gName;
    private Hashtable gCache = new Hashtable();

    private int gCacheCount = 0;
    private int gInteractions = 0;
    private int gSize = 100;
    private int gMinLifeTime = 1 * 60 * 1000;
    private int gMaxLifeTime = 5 * 60 * 1000;
    private int gMaxInteractions = 250;

////////////////////////////////////////////////////////////////////////////////
//  Constructor
////////////////////////////////////////////////////////////////////////////////
    private Caching(String pName)
    {
        gName = pName;
    }

////////////////////////////////////////////////////////////////////////////////
//  Methods
////////////////////////////////////////////////////////////////////////////////

    public static Caching getCache(String pName)
    {
        Caching vCaching = (Caching)gCaches.get(pName);
        if (vCaching == null)
        {
            vCaching = new Caching(pName);
            gCaches.put(pName, vCaching);
        }
        return vCaching;
    }

    public static Caching getCache(String pName, int pMinLifeTime, int pMaxLifeTime, int pSize)
    {
        Caching vCaching = getCache(pName);
        vCaching.setMinLifeTime(pMinLifeTime);
        vCaching.setMaxLifeTime(pMaxLifeTime);
        vCaching.gSize = pSize;
        return vCaching;
    }

     public static Caching getCache(String pName, int pMinLifeTime, int pMaxLifeTime, int pSize, int pMaxInteractions)
    {
        Caching vCaching = getCache(pName);
        vCaching.setMinLifeTime(pMinLifeTime);
        vCaching.setMaxLifeTime(pMaxLifeTime);
        vCaching.gSize = pSize;
        vCaching.setMaxInteractions(pMaxInteractions);
        return vCaching;
    }

    public String getName()
    {
        return gName;
    }

    public synchronized Object get(Object pKey)
    {
        gInteractions++;
        CacheObject vCacheObject = (CacheObject)gCache.get(pKey);
        if (vCacheObject == null)
        {
            gLog.info(gCacheCount + "\tget(" + pKey + ") NOT IN CACHE ");
            return null;
        }
        else
        {
            if (hasMaxAge(vCacheObject))
            {
                gCacheCount--;
                gLog.debug(gCacheCount + "\tREMOVE " + vCacheObject);
                gLog.info(gCacheCount + "\tget(" + pKey + ") NOT IN CACHE ");
                gCache.remove(pKey);
                return null;
            }
            else
            {
                gLog.info(gCacheCount + "\tget(" + pKey + ") " + vCacheObject);
            }
        }
        vCacheObject.hit();
        if (gInteractions > gMaxInteractions){
              cleanCache();
        }
        return vCacheObject.getObject();
    }

    public synchronized Object remove(Object pKey)
    {
        Object vObject = gCache.remove(pKey);
        if (vObject != null)
        {
            gCacheCount--;
        }
        return vObject;
    }

    public synchronized long getAge(Object pKey)
    {
        CacheObject vCacheObject = (CacheObject)gCache.get(pKey);
        if (vCacheObject == null)
        {
            gLog.info(gCacheCount + "\tgetAge(" + pKey + ") NOT IN CACHE ");
            throw new RuntimeException("Object " + pKey + " NOT IN CACHE");
        }
        else
        {
            return vCacheObject.getAge();
        }
    }

    public synchronized void put(Object pKey, Object pObject)
    {
        gInteractions++;
        CacheObject vCacheObject = (CacheObject)gCache.get(pKey);
        if (vCacheObject != null)
        {
            return;
        }
        if (gInteractions < gMaxInteractions && gCacheCount < gSize)
        {
            gCacheCount++;
            vCacheObject = new CacheObject(pKey, pObject);
            gLog.debug(gCacheCount + " " + gInteractions + "/" + gMaxInteractions + "\tADD    " + vCacheObject);
            vCacheObject.hit();
            gCache.put(pKey, vCacheObject);
        }
        else
        {
            cleanCache();
            if (gCacheCount < gSize)
            {
                gCacheCount++;
                vCacheObject = new CacheObject(pKey, pObject);
                gLog.debug(gCacheCount + " " + gInteractions + "/" + gMaxInteractions + "\tADD    " + vCacheObject);
                vCacheObject.hit();
                gCache.put(pKey, vCacheObject);
            }
        }
    }

    public synchronized void reset()
    {
        Enumeration vKeys = gCache.keys();
        while(vKeys.hasMoreElements())
        {
            Object vKey = vKeys.nextElement();
            gCache.remove(vKey);
        }
        gCache = new Hashtable();
    }

    private void cleanCache()
    {
        gInteractions = 0;
        Object[] vObject = gCache.values().toArray();
        Arrays.sort(vObject);
        boolean vPlaceFree = false;
        for (int i = 0; i < vObject.length; i++)
        {
            CacheObject vCacheObject = (CacheObject)vObject[i];
            if (!vPlaceFree && hasMinAge(vCacheObject) && i < vObject.length * 0.66)
            {
                gCacheCount--;
                gLog.debug(gCacheCount + "\tREMOVE " + vCacheObject);
                gCache.remove(vCacheObject.getKey());
                vPlaceFree = true;
            }
            else if (hasMaxAge(vCacheObject))
            {
                gCacheCount--;
                gLog.debug(gCacheCount + "\tREMOVE " + vCacheObject);
                gCache.remove(vCacheObject.getKey());
                vPlaceFree = true;
            }
        }
    }

    private boolean hasMinAge(CacheObject vCacheObject)
    {
        return vCacheObject.getAge() > (long) gMinLifeTime;
    }

    private boolean hasMaxAge(CacheObject vCacheObject)
    {
        return vCacheObject.getAge() > (long) gMaxLifeTime;
    }

    public void setSize(int pSize)
    {
        gSize = pSize;
    }

     public void setMinLifeTime(int pMinLifeTime)
    {
        gMinLifeTime = pMinLifeTime * 60 * 1000;
    }


    public void setMaxInteractions(int pMaxInteractions)
    {
        gMaxInteractions = pMaxInteractions;
    }

    public void setMaxLifeTime(int pMaxLifeTime)
    {
        if (pMaxLifeTime == Integer.MAX_VALUE)
        {
            gMaxLifeTime = Integer.MAX_VALUE;
        }
        else
        {
            gMaxLifeTime = pMaxLifeTime * 60 * 1000;
        }
    }
    public int getCurrentSize()
    {
        return gCacheCount;
    }

////////////////////////////////////////////////////////////////////////////////
//  Inner Class
////////////////////////////////////////////////////////////////////////////////
    private class CacheObject implements Comparable
    {
        ///////////////////////////////////////////////////////////////
        // Internal Fields
        ///////////////////////////////////////////////////////////////
        private Object gKey;
        private Object gObject;
        private long gCreationTime;
        private int gHits;

        ///////////////////////////////////////////////////////////////
        // Constructor
        ///////////////////////////////////////////////////////////////
        CacheObject(Object pKey, Object pObject)
        {
            gKey = pKey;
            gObject = pObject;
            gCreationTime = System.currentTimeMillis();
            gHits = 0;
        }

        public long getAge()
        {
            return System.currentTimeMillis() - gCreationTime;
        }

        public float getHitRate()
        {
            float vHitRate = 0.0f;
            long vAge = getAge();
            if (vAge != 0L && vAge < (long) (gMaxLifeTime + gMinLifeTime))
            {
                vHitRate = ((float)(gHits * 60 * 1000)) / (float) getAge();
            }
            return vHitRate;
        }

        public void hit()
        {
            gHits++;
        }

        public Object getKey()
        {
            return gKey;
        }

        public Object getObject()
        {
            return gObject;
        }

        public String toString()
        {
            return "CACHE:" + gKey + " age:" + getAgeFormat() +
                    " hitrate:" + getHitRate() + "hit/min";
        }

        private String getAgeFormat()
        {
            long vAge = getAge();
            long vSec = (vAge / 1000) % 60;
            long vMin = vAge / 60000;
            return vMin + ":" + vSec;
        }

        ///////////////////////////////////////////////////////////////
        // Implements Comparable
        ///////////////////////////////////////////////////////////////

        public int compareTo(Object vObject)
        {
            CacheObject vCacheObject = (CacheObject)vObject;
            return (int)((getHitRate() * 100) - (vCacheObject.getHitRate() * 100));
        }
    }
}
[+][-]09/30/05 09:42 AM, ID: 14993889Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zones: Java Programming Language, New to Java Programming
Tags: memory, leak
Sign Up Now!
Solution Provided By: sciuriware
Participating Experts: 4
Solution Grade: B
 
[+][-]09/30/05 05:44 AM, ID: 14991508Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/30/05 06:01 AM, ID: 14991650Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09/30/05 06:20 AM, ID: 14991831Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/30/05 07:49 AM, ID: 14992793Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09/30/05 07:59 AM, ID: 14992891Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/30/05 08:01 AM, ID: 14992930Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/30/05 08:32 AM, ID: 14993260Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/30/05 02:17 PM, ID: 14996239Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/30/05 11:49 PM, ID: 14997794Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/03/05 12:36 AM, ID: 15004745Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/03/05 12:38 AM, ID: 15004749Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-92