1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-25 18:05:33 +02:00

[cleanup] fix threshold computation

This commit is contained in:
Martin Oberhuber 2009-02-06 09:32:53 +00:00
parent d2da4a8981
commit d775569a41

View file

@ -8,7 +8,7 @@
* Initial Contributors: * Initial Contributors:
* The following IBM employees contributed to the Remote System Explorer * The following IBM employees contributed to the Remote System Explorer
* component that contains this file: David McKnight * component that contains this file: David McKnight
* *
* Contributors: * Contributors:
* David McKnight (IBM) - [261644] [dstore] remote search improvements * David McKnight (IBM) - [261644] [dstore] remote search improvements
********************************************************************************/ ********************************************************************************/
@ -26,66 +26,66 @@ public class MemoryManager {
private Object mbean; private Object mbean;
private static MemoryManager _instance; private static MemoryManager _instance;
private DataStore _dataStore; private DataStore _dataStore;
private MemoryManager(DataStore dataStore) { private MemoryManager(DataStore dataStore) {
init(); init();
_dataStore = dataStore; _dataStore = dataStore;
} }
public static MemoryManager getInstance(DataStore dataStore){ public static MemoryManager getInstance(DataStore dataStore){
if (_instance == null){ if (_instance == null){
_instance = new MemoryManager(dataStore); _instance = new MemoryManager(dataStore);
} }
return _instance; return _instance;
} }
private void init(){ private void init(){
String thresholdString = System.getProperty("search.threshold"); //$NON-NLS-1$ String thresholdString = System.getProperty("search.threshold"); //$NON-NLS-1$
double threshold = 0.8; double threshold = 0.8;
if(thresholdString != null && thresholdString.length() > 0) { if(thresholdString != null && thresholdString.length() > 0) {
threshold = (long) (Integer.parseInt(thresholdString) / 100); threshold = Integer.parseInt(thresholdString) / 100.0;
} }
// do we have java 1.5? // do we have java 1.5?
try { try {
Class factoryClass = Class.forName("java.lang.management.ManagementFactory"); //$NON-NLS-1$ Class factoryClass = Class.forName("java.lang.management.ManagementFactory"); //$NON-NLS-1$
Method method = factoryClass.getDeclaredMethod("getMemoryPoolMXBeans", new Class[0]); //$NON-NLS-1$ Method method = factoryClass.getDeclaredMethod("getMemoryPoolMXBeans", new Class[0]); //$NON-NLS-1$
List list = (List)method.invoke(null, null); List list = (List)method.invoke(null, null);
for(int i = 0;i < list.size(); i++) { for(int i = 0;i < list.size(); i++) {
Object mbObj = list.get(i); Object mbObj = list.get(i);
Class mbClass = mbObj.getClass(); Class mbClass = mbObj.getClass();
Method getSupportedMethod = mbClass.getDeclaredMethod("isUsageThresholdSupported", new Class[0]); //$NON-NLS-1$ Method getSupportedMethod = mbClass.getDeclaredMethod("isUsageThresholdSupported", new Class[0]); //$NON-NLS-1$
Boolean usageThresholdSupported = (Boolean)getSupportedMethod.invoke(mbObj, null); Boolean usageThresholdSupported = (Boolean)getSupportedMethod.invoke(mbObj, null);
if (usageThresholdSupported.booleanValue()){ if (usageThresholdSupported.booleanValue()){
Method getTypeMethod = mbClass.getDeclaredMethod("getType", new Class[0]); //$NON-NLS-1$ Method getTypeMethod = mbClass.getDeclaredMethod("getType", new Class[0]); //$NON-NLS-1$
Object typeObj = getTypeMethod.invoke(mbObj, null); Object typeObj = getTypeMethod.invoke(mbObj, null);
Class memoryType = Class.forName("java.lang.management.MemoryType"); //$NON-NLS-1$ Class memoryType = Class.forName("java.lang.management.MemoryType"); //$NON-NLS-1$
Field field = memoryType.getField("HEAP"); //$NON-NLS-1$ Field field = memoryType.getField("HEAP"); //$NON-NLS-1$
Object fieldObj = field.get(typeObj); Object fieldObj = field.get(typeObj);
if (fieldObj.equals(typeObj)){ if (fieldObj.equals(typeObj)){
Method getUsageMethod = mbClass.getDeclaredMethod("getUsage", new Class[0]); //$NON-NLS-1$ Method getUsageMethod = mbClass.getDeclaredMethod("getUsage", new Class[0]); //$NON-NLS-1$
Object usageObj = getUsageMethod.invoke(mbObj, null); Object usageObj = getUsageMethod.invoke(mbObj, null);
Class usageClass = usageObj.getClass(); Class usageClass = usageObj.getClass();
Method getMaxMethod = usageClass.getDeclaredMethod("getMax", new Class[0]); //$NON-NLS-1$ Method getMaxMethod = usageClass.getDeclaredMethod("getMax", new Class[0]); //$NON-NLS-1$
Long maxObj = (Long)getMaxMethod.invoke(usageObj, null); Long maxObj = (Long)getMaxMethod.invoke(usageObj, null);
Method setThresholdMethod = mbClass.getDeclaredMethod("setUsageThreshold", new Class[] { long.class }); //$NON-NLS-1$ Method setThresholdMethod = mbClass.getDeclaredMethod("setUsageThreshold", new Class[] { long.class }); //$NON-NLS-1$
Object[] args = new Object[1]; Object[] args = new Object[1];
args[0] = new Long((long)(maxObj.longValue() * threshold)); args[0] = new Long((long)(maxObj.longValue() * threshold));
setThresholdMethod.invoke(mbObj, args); setThresholdMethod.invoke(mbObj, args);
mbean = mbObj; mbean = mbObj;
break; break;
} }
} }
} }
} }
catch (Exception e){ catch (Exception e){
@ -93,9 +93,9 @@ public class MemoryManager {
e.printStackTrace(); e.printStackTrace();
} }
} }
public boolean isThresholdExceeded() { public boolean isThresholdExceeded() {
if (mbean != null){ if (mbean != null){
try { try {
Method method = mbean.getClass().getMethod("isUsageThresholdExceeded", new Class[0]); //$NON-NLS-1$ Method method = mbean.getClass().getMethod("isUsageThresholdExceeded", new Class[0]); //$NON-NLS-1$
@ -106,7 +106,7 @@ public class MemoryManager {
return false; return false;
} }
} }
else { else {
// no Java 1.5 available, so this is the fallback // no Java 1.5 available, so this is the fallback
Runtime runtime = Runtime.getRuntime(); Runtime runtime = Runtime.getRuntime();
long freeMem = runtime.freeMemory(); long freeMem = runtime.freeMemory();
@ -114,11 +114,11 @@ public class MemoryManager {
if (freeMem < 10000){ if (freeMem < 10000){
return true; return true;
} }
return false; return false;
} }
} }
public void checkAndClearupMemory() public void checkAndClearupMemory()
{ {
int count = 0; int count = 0;
@ -131,11 +131,11 @@ public class MemoryManager {
count ++; count ++;
_dataStore.trace("CLEAN free mem="+Runtime.getRuntime().freeMemory()); //$NON-NLS-1$ _dataStore.trace("CLEAN free mem="+Runtime.getRuntime().freeMemory()); //$NON-NLS-1$
} }
if(count == 5) { if(count == 5) {
_dataStore.trace("Out of memory - shutting down"); //$NON-NLS-1$ _dataStore.trace("Out of memory - shutting down"); //$NON-NLS-1$
Exception e = new Exception(); Exception e = new Exception();
_dataStore.trace(e); _dataStore.trace(e);
@ -151,6 +151,6 @@ public class MemoryManager {
if (SystemServiceManager.getInstance().getSystemService() == null) if (SystemServiceManager.getInstance().getSystemService() == null)
System.exit(-1); System.exit(-1);
} }
} }
} }