01: /*
02: * <copyright>
03: *
04: * Copyright 2004 BBNT Solutions, LLC
05: * under sponsorship of the Defense Advanced Research Projects
06: * Agency (DARPA).
07: *
08: * You can redistribute this software and/or modify it under the
09: * terms of the Cougaar Open Source License as published on the
10: * Cougaar Open Source Website (www.cougaar.org).
11: *
12: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
13: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
14: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
15: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
16: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
17: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
18: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23: *
24: * </copyright>
25: */
26:
27: package org.cougaar.util;
28:
29: import org.cougaar.bootstrap.SystemProperties;
30: import org.cougaar.util.log.Logger;
31: import org.cougaar.util.log.Logging;
32:
33: /**
34: * A utility for requesting a GC in various ways.
35: */
36: public class GC {
37: // uninstantiable
38: private GC() {
39: }
40:
41: /** Static logger for GC **/
42: private static Logger log = Logging.getLogger(GC.class);
43:
44: /** lock for GC timer **/
45: private static final Object gcLock = new Object();
46:
47: /** when did we last do a GC, guarded by gcLock **/
48: private static long gcTime = 0L;
49:
50: public static final long minGCInterval_DEFAULT = 5 * 60 * 1000L;
51: public static final String minGCInterval_DEFPROP = "org.cougaar.core.persistence.lazyInterval";
52: public static final String minGCInterval_PROP = "org.cougaar.util.GC.minGCInterval";
53: private static final long minGCInterval = SystemProperties.getLong(
54: minGCInterval_PROP, SystemProperties.getLong(
55: minGCInterval_DEFPROP, minGCInterval_DEFAULT));
56:
57: /** Invoke System.gc(), subject to policy constraints:
58: * e.g. if it has been at least minGCInterval since
59: * the last gc call completed.
60: *
61: * At INFO logging level, will log when gcs are deferred and allowed.
62: * At DEBUG logging lovel, will log how long allowed GCs run for.
63: *
64: * @property org.cougaar.util.GC.minGCInterval
65: * Minimum number of milliseconds between IdentityTable-directed gcs.
66: * Defaults to 300000 (5 minutes). Garbage Collections not invoked
67: * through this interface are invisible. The default value
68: * is actually supplied by the property org.cougaar.core.persistence.lazyInterval,
69: * if available.
70: **/
71: public static final void gc() {
72: long now = System.currentTimeMillis();
73: synchronized (gcLock) {
74: if ((gcTime != 0L) && ((now - gcTime) < minGCInterval)) {
75: if (log.isInfoEnabled()) {
76: log.info("deferred explicit GC");
77: }
78: return;
79: }
80:
81: System.gc();
82:
83: if (log.isInfoEnabled()) {
84: log.info("explicit GC");
85: }
86: long tend = System.currentTimeMillis();
87: gcTime = tend;
88: if (log.isDebugEnabled()) {
89: log
90: .debug("GC.gc() ran for " + (tend - now)
91: + " millis");
92: }
93: }
94: }
95: }
|