001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/monitor/CmsMemoryStatus.java,v $
003: * Date : $Date: 2008-02-27 12:05:49 $
004: * Version: $Revision: 1.10 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software GmbH, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.monitor;
033:
034: /**
035: * Data structure for dealing with memory status information.<p>
036: *
037: * @author Alexander Kandzior
038: *
039: * @version $Revision: 1.10 $
040: *
041: * @since 6.0.0
042: */
043: class CmsMemoryStatus {
044:
045: private int m_count;
046: private long m_freeMemory;
047:
048: private long m_maxMemory;
049: private long m_totalMemory;
050: private long m_usage;
051: private long m_usedMemory;
052:
053: /**
054: * Initializes a new instance of the memory status with the current memory values.<p>
055: */
056: public CmsMemoryStatus() {
057:
058: update();
059: }
060:
061: /**
062: * Calculates the average memory consumption by updating the stored information with
063: * the provided current information.<p>
064: *
065: * @param currentStatus the memory status to update the average with
066: */
067: public void calculateAverage(CmsMemoryStatus currentStatus) {
068:
069: int newCount = m_count + 1;
070: m_maxMemory = ((m_count * m_maxMemory) + currentStatus
071: .getMaxMemory())
072: / newCount;
073: m_totalMemory = ((m_count * m_totalMemory) + currentStatus
074: .getTotalMemory())
075: / newCount;
076: m_usedMemory = ((m_count * m_usedMemory) + currentStatus
077: .getUsedMemory())
078: / newCount;
079: m_freeMemory = ((m_count * m_freeMemory) + currentStatus
080: .getFreeMemory())
081: / newCount;
082: m_usage = m_usedMemory * 100 / m_maxMemory;
083: m_count = newCount;
084: }
085:
086: /**
087: * Returns the count used to calculate the average.<p>
088: *
089: * @return the count used to calculate the average
090: */
091: public int getCount() {
092:
093: return m_count;
094: }
095:
096: /**
097: * Returns the current free memory, in megabytes.<p>
098: *
099: * @return the current free memory, in megabytes
100: */
101: public long getFreeMemory() {
102:
103: return m_freeMemory;
104: }
105:
106: /**
107: * Returns the maximum available memory, in megabytes.<p>
108: *
109: * @return the maximum available memory, in megabytes
110: */
111: public long getMaxMemory() {
112:
113: return m_maxMemory;
114: }
115:
116: /**
117: * Returns the amount of memory currently availble to the JVM, in megabytes.<p>
118: *
119: * @return the amount of memory currently availble to the JVM, in megabytes
120: */
121: public long getTotalMemory() {
122:
123: return m_totalMemory;
124: }
125:
126: /**
127: * Returns the current memory usage, in percent.<p>
128: *
129: * @return the current memory usage, in percent
130: */
131: public long getUsage() {
132:
133: return m_usage;
134: }
135:
136: /**
137: * Returns the amount of memory currently used, in megabytes.<p>
138: *
139: * @return the amount of memory currently used, in megabytes
140: */
141: public long getUsedMemory() {
142:
143: return m_usedMemory;
144: }
145:
146: /**
147: * Updates this memory status with the current memory information.<p>
148: */
149: public void update() {
150:
151: m_maxMemory = Runtime.getRuntime().maxMemory() / 1048576;
152: m_totalMemory = Runtime.getRuntime().totalMemory() / 1048576;
153: m_usedMemory = (Runtime.getRuntime().totalMemory() - Runtime
154: .getRuntime().freeMemory()) / 1048576;
155: m_freeMemory = m_maxMemory - m_usedMemory;
156: m_usage = m_usedMemory * 100 / m_maxMemory;
157: }
158: }
|