001: /*
002: * Created on Aug 3, 2004
003: *
004: * To change the template for this generated file go to
005: * Window - Preferences - Java - Code Generation - Code and Comments
006: */
007: package de.laures.cewolf.storage;
008:
009: import java.util.Calendar;
010: import java.util.Collection;
011: import java.util.Date;
012: import java.util.GregorianCalendar;
013: import java.util.HashMap;
014: import java.util.Iterator;
015: import java.util.Map;
016:
017: /**
018: * @author brianf
019: *
020: * To change the template for this generated type comment go to Window -
021: * Preferences - Java - Code Generation - Code and Comments
022: */
023: public class SessionStorageGroup implements Runnable {
024: private Map map = new HashMap();
025: Thread runner;
026:
027: private void start() {
028: if (runner == null || !runner.isAlive()) {
029: runner = new Thread(this );
030: runner.setDaemon(false);
031: runner.setName("SessionCleanup");
032: runner.setPriority(Thread.MIN_PRIORITY);
033: runner.start();
034: }
035: }
036:
037: public synchronized Object get(Object a) {
038: return map.get(a);
039: }
040:
041: public synchronized Object put(Object a, Object b) {
042: Object c = map.put(a, b);
043: start();
044: return c;
045: }
046:
047: public synchronized Object remove(Object a) {
048: return map.remove(a);
049: }
050:
051: /*
052: * (non-Javadoc)
053: *
054: * @see java.lang.Runnable#run()
055: */
056: public void run() {
057: while (!map.isEmpty()) {
058: Date date = new Date();
059: synchronized (this ) {
060: Collection keys = map.keySet();
061:
062: Iterator iter = keys.iterator();
063:
064: while (iter.hasNext()) {
065: // System.out.println("Get Next");
066: String cid = (String) iter.next();
067: SessionStorageItem ssi = (SessionStorageItem) get(cid);
068: if (ssi.isExpired(date)) {
069: // System.out.println("Removing " + ssi);
070: iter.remove();
071: }
072: }
073: }
074: try {
075: //System.gc();
076: Thread.sleep(1000);
077: } catch (InterruptedException e) {
078: // TODO Auto-generated catch block
079: e.printStackTrace();
080: }
081: }
082: // System.out.println("Exiting Thread");
083: }
084:
085: public static void main(String[] args) {
086: int start = 0;
087: while (true) {
088: System.out.println("Adding Objects");
089: testThread(start);
090:
091: try {
092: Thread.sleep(10000);
093: } catch (InterruptedException e) {
094: // TODO Auto-generated catch block
095: e.printStackTrace();
096: }
097: }
098: }
099:
100: public static void testThread(int start) {
101: SessionStorageGroup ssg = new SessionStorageGroup();
102: Calendar cal = new GregorianCalendar();
103: cal.setTime(new Date());
104: // some huge garbage string
105:
106: for (int i = start; i < start + 10000; i++) {
107: cal.add(Calendar.MILLISECOND, 1);
108: SessionStorageItem ssi = new SessionStorageItem(null,
109: Integer.toString(i), cal.getTime());
110: ssg.put(Integer.toString(i), ssi);
111: }
112:
113: }
114:
115: /**
116: * Test that memory is freed up when low on it...
117: */
118: public static void testSoftreferenceMemoryFreeup() {
119: Date neverexpire = new Date(10000, 1, 1);
120: SessionStorageGroup ssg = new SessionStorageGroup();
121:
122: // some huge garbage string
123: StringBuffer longString = new StringBuffer();
124: for (int i = 0; i < 10000; i++) {
125: longString.append(Math.random());
126: }
127:
128: int i = 0;
129: long minmem = Long.MAX_VALUE;
130: while (true) {
131: // let's use the id string to waste memory
132: String key = Long.toString(System.currentTimeMillis());
133: String id = key + longString;
134: SessionStorageItem ssi = new SessionStorageItem(null, id,
135: neverexpire);
136: ssg.put(key, ssi);
137:
138: i++;
139: long freemem = Runtime.getRuntime().freeMemory();
140: long usedmem = Runtime.getRuntime().totalMemory();
141: long maxmem = Runtime.getRuntime().maxMemory();
142: if (freemem < minmem) {
143: minmem = freemem;
144: }
145: if (i % 100 == 0) {
146: System.out
147: .println("#" + i + ", minimum memory:" + minmem
148: + ", freemem:" + freemem + ", usedmem:"
149: + usedmem + ", maxmem:" + maxmem);
150: }
151: }
152:
153: }
154: }
|