KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > monitor > CmsMemoryStatus


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/monitor/CmsMemoryStatus.java,v $
3  * Date : $Date: 2005/06/23 11:11:38 $
4  * Version: $Revision: 1.7 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.monitor;
33
34 /**
35  * Data structure for dealing with memory status information.<p>
36  *
37  * @author Alexander Kandzior
38  *
39  * @version $Revision: 1.7 $
40  *
41  * @since 6.0.0
42  */

43 class CmsMemoryStatus {
44     private int m_count;
45     private long m_freeMemory;
46
47     private long m_maxMemory;
48     private long m_totalMemory;
49     private long m_usage;
50     private long m_usedMemory;
51
52     /**
53      * Initializes a new instance of the memory status with the current memory values.<p>
54      */

55     public CmsMemoryStatus() {
56
57         update();
58     }
59
60     /**
61      * Calculates the average memory consumption by updating the stored information with
62      * the provided current information.<p>
63      *
64      * @param currentStatus the memory status to update the average with
65      */

66     public void calculateAverage(CmsMemoryStatus currentStatus) {
67
68         int newCount = m_count + 1;
69         m_maxMemory = ((m_count * m_maxMemory) + currentStatus.getMaxMemory()) / newCount;
70         m_totalMemory = ((m_count * m_totalMemory) + currentStatus.getTotalMemory()) / newCount;
71         m_usedMemory = ((m_count * m_usedMemory) + currentStatus.getUsedMemory()) / newCount;
72         m_freeMemory = ((m_count * m_freeMemory) + currentStatus.getFreeMemory()) / newCount;
73         m_usage = m_usedMemory * 100 / m_maxMemory;
74         m_count = newCount;
75     }
76
77     /**
78      * Returns the count used to calculate the average.<p>
79      *
80      * @return the count used to calculate the average
81      */

82     public int getCount() {
83
84         return m_count;
85     }
86
87     /**
88      * Returns the current free memory, in megabytes.<p>
89      *
90      * @return the current free memory, in megabytes
91      */

92     public long getFreeMemory() {
93
94         return m_freeMemory;
95     }
96
97     /**
98      * Returns the maximum available memory, in megabytes.<p>
99      *
100      * @return the maximum available memory, in megabytes
101      */

102     public long getMaxMemory() {
103
104         return m_maxMemory;
105     }
106
107     /**
108      * Returns the amount of memory currently availble to the JVM, in megabytes.<p>
109      *
110      * @return the amount of memory currently availble to the JVM, in megabytes
111      */

112     public long getTotalMemory() {
113
114         return m_totalMemory;
115     }
116
117     /**
118      * Returns the current memory usage, in percent.<p>
119      *
120      * @return the current memory usage, in percent
121      */

122     public long getUsage() {
123
124         return m_usage;
125     }
126
127     /**
128      * Returns the amount of memory currently used, in megabytes.<p>
129      *
130      * @return the amount of memory currently used, in megabytes
131      */

132     public long getUsedMemory() {
133
134         return m_usedMemory;
135     }
136
137     /**
138      * Updates this memory status with the current memory information.<p>
139      */

140     public void update() {
141
142         m_maxMemory = Runtime.getRuntime().maxMemory() / 1048576;
143         m_totalMemory = Runtime.getRuntime().totalMemory() / 1048576;
144         m_usedMemory = (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1048576;
145         m_freeMemory = m_maxMemory - m_usedMemory;
146         m_usage = m_usedMemory * 100 / m_maxMemory;
147     }
148 }
149
Popular Tags