froehlich 02/01/28 11:57:52
Modified: src/java/org/apache/cocoon/components/store
StoreJanitorImpl.java MRUMemoryStore.java
Log:
ok I hope I fixed the problem now:
The number of objects which are freed out of the Store
is calculated now. It take a configurable percentage
of stored objects. For i.e: 10% and 1000 objects, then
the StoreJanitor free out 100 objects!
Revision Changes Path
1.6 +72 -12
xml-cocoon2/src/java/org/apache/cocoon/components/store/StoreJanitorImpl.java
Index: StoreJanitorImpl.java
===================================================================
RCS file:
/home/cvs/xml-cocoon2/src/java/org/apache/cocoon/components/store/StoreJanitorImpl.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- StoreJanitorImpl.java 27 Jan 2002 17:54:18 -0000 1.5
+++ StoreJanitorImpl.java 28 Jan 2002 19:57:52 -0000 1.6
@@ -16,6 +16,7 @@
import org.apache.avalon.framework.thread.ThreadSafe;
import java.util.ArrayList;
+import java.util.Enumeration;
import java.util.Iterator;
/**
@@ -43,6 +44,7 @@
private ArrayList storelist;
private int index = -1;
private static boolean doRun = false;
+ private int m_percent;
/**
* Initialize the StoreJanitorImpl.
@@ -69,6 +71,7 @@
this.setCleanupthreadinterval(params.getParameterAsInteger("cleanupthreadinterval",10));
this.setPriority(params.getParameterAsInteger( "threadpriority",
Thread.currentThread().getPriority()));
+ this.m_percent = params.getParameterAsInteger( "percent_to_free",10);
if ((this.getFreememory() < 1)) {
throw new ConfigurationException("StoreJanitorImpl freememory parameter
has to be greater then 1");
@@ -82,6 +85,9 @@
if ((this.getPriority() < 1)) {
throw new ConfigurationException("StoreJanitorImpl threadpriority has
to be greater then 1");
}
+ if ((this.m_percent > 100 && this.m_percent < 1)) {
+ throw new ConfigurationException("StoreJanitorImpl percent_to_free, has
to be between 1 and 100");
+ }
this.setStoreList(new ArrayList());
}
@@ -109,27 +115,26 @@
while (doRun) {
// amount of memory used is greater then heapsize
if (this.memoryLow()) {
- int cnt = 0;
-
if (this.getLogger().isDebugEnabled()) {
this.getLogger().debug("Invoking garbage collection, total
memory = "
+ this.getJVM().totalMemory() + ", free memory = "
+ this.getJVM().freeMemory());
}
+
this.freePhysicalMemory();
+
if (this.getLogger().isDebugEnabled()) {
this.getLogger().debug("Garbage collection complete, total
memory = "
+ this.getJVM().totalMemory() + ", free memory = "
+ this.getJVM().freeMemory());
}
+
synchronized (this) {
- while (this.memoryLow()
- && this.getStoreList().size() > 0
- && cnt < 1) {
+ if (this.memoryLow() && this.getStoreList().size() > 0) {
this.freeMemory();
- cnt++;
+ this.setIndex(this.getIndex() + 1);
}
- }
+ }
}
try {
Thread.currentThread().sleep(this.cleanupthreadinterval * 1000);
@@ -210,7 +215,10 @@
* Round Robin alghorithm for freeing the registerd caches.
*/
private void freeMemory() {
+ Store store;
+
try {
+ //Determine elements in Store:
if (this.getLogger().isDebugEnabled()) {
this.getLogger().debug("StoreList size=" +
this.getStoreList().size());
this.getLogger().debug("Actual Index position: " + this.getIndex());
@@ -218,31 +226,83 @@
if (this.getIndex() < this.getStoreList().size()) {
if(this.getIndex() == -1) {
this.setIndex(0);
+ store = (Store)this.getStoreList().get(this.getIndex());
+
if (this.getLogger().isDebugEnabled()) {
this.getLogger().debug("Freeing Store: " + this.getIndex());
}
- ((Store)this.getStoreList().get(this.getIndex())).free();
+
+ //delete proportionate elements out of the cache as
+ //configured.
+ int limit = this.calcToFree(store);
+ for (int i=0; i < limit; i++) {
+ store.free();
+ }
} else {
- this.setIndex(this.getIndex() + 1);
+ store = (Store)this.getStoreList().get(this.getIndex());
+
if (this.getLogger().isDebugEnabled()) {
this.getLogger().debug("Freeing Store: " + this.getIndex());
}
- ((Store)this.getStoreList().get(this.getIndex())).free();
+
+ //delete proportionate elements out of the cache as
+ //configured.
+ int limit = this.calcToFree(store);
+ for (int i=0; i < limit; i++) {
+ store.free();
+ }
}
} else {
if (this.getLogger().isDebugEnabled()) {
this.getLogger().debug("Starting from the beginning");
}
+
this.resetIndex();
- ((Store)this.getStoreList().get(0)).free();
this.setIndex(0);
+ store = (Store)this.getStoreList().get(this.getIndex());
+
+ //delete proportionate elements out of the cache as
+ //configured.
+ int limit = this.calcToFree(store);
+ for (int i=0; i < limit; i++) {
+ store.free();
+ }
}
- //this.freePhysicalMemory();
} catch(Exception e) {
this.getLogger().error("Error in freeMemory()",e);
}
}
+ /**
+ * This method claculates the number of Elements to be freememory
+ * out of the Cache.
+ *
+ * @param store the Store which was selected as victim
+ * @return number of elements to be removed!
+ */
+ private int calcToFree(Store store) {
+ int cnt = 0;
+ try {
+ this.getLogger().debug("Calculating percentage!");
+ Enumeration enum = store.keys();
+
+ while(enum.hasMoreElements()) {
+ cnt++;
+ enum.nextElement();
+ }
+
+ this.getLogger().debug("Counted: " + cnt);
+ double fac = new Integer(this.m_percent).doubleValue() / 100;
+ double dcnt = new Integer(cnt).doubleValue();
+ this.getLogger().debug("Calculated: " + new Double(dcnt *
fac).intValue());
+
+ return new Double(dcnt * fac).intValue();
+ } catch (Exception e) {
+ this.getLogger().error("",e);
+ return 0;
+ }
+ }
+
/**
* This method forces the garbage collector
*/
1.4 +1 -54
xml-cocoon2/src/java/org/apache/cocoon/components/store/MRUMemoryStore.java
Index: MRUMemoryStore.java
===================================================================
RCS file:
/home/cvs/xml-cocoon2/src/java/org/apache/cocoon/components/store/MRUMemoryStore.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- MRUMemoryStore.java 27 Jan 2002 13:14:40 -0000 1.3
+++ MRUMemoryStore.java 28 Jan 2002 19:57:52 -0000 1.4
@@ -264,7 +264,7 @@
* @return the enumeration of the cache
*/
public Enumeration keys() {
- return new StoreEnumeration(this.cache.keys(),
this.fsstore.keys(),this.cachedirstr);
+ return this.cache.keys();
}
/**
@@ -340,58 +340,5 @@
.append(URLEncoder.encode(key.toString()))
.toString();
}
-
- final class StoreEnumeration implements Enumeration {
-
- private String[] array;
- private int index;
- private int length;
-
- StoreEnumeration(Enumeration cache, Enumeration fs, String cachedir) {
- this.array = new String[16];
- this.length = 0;
- this.index = 0;
-
- while (cache.hasMoreElements()) {
- this.add(cache.nextElement().toString());
- }
- RE re = null;
-
- try {
- re = new RE("^" + cachedir);
- } catch(RESyntaxException ree) {
- ree.printStackTrace();
- }
-
- while (fs.hasMoreElements()) {
- final String key = fs.nextElement().toString();
- if (re.match(cachedir)) {
- this.add(key);
- }
- }
- }
-
- public void add(String key) {
- if (this.length == array.length) {
- String[] newarray = new String[this.length + 16];
- System.arraycopy(this.array, 0, newarray, 0, this.array.length);
- this.array = newarray;
- }
- this.array[this.length] = key;
- this.length++;
- }
-
- public boolean hasMoreElements() {
- return (this.index < this.length);
- }
-
- public Object nextElement() {
- if (this.hasMoreElements()) {
- this.index++;
- return this.array[index-1];
- }
- return null;
- }
- }
}
----------------------------------------------------------------------
In case of troubles, e-mail: [EMAIL PROTECTED]
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]