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