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