private boolean checkAvaliableMemory(int neededMemory, TestResult testResult) { if ((ourMode & CHECK_MEMORY) == 0) return true; boolean possibleOutOfMemoryError = possibleOutOfMemory(neededMemory); if (possibleOutOfMemoryError) { tryGc(5); possibleOutOfMemoryError = possibleOutOfMemory(neededMemory); if (possibleOutOfMemoryError) { log("OutOfMemoryError: dumping memory"); Runtime runtime = Runtime.getRuntime(); long total = runtime.totalMemory(); long free = runtime.freeMemory(); String errorMessage = "Too much memory used. Total: " + total + " free: " + free + " used: " + (total - free) + "\n"; addErrorMessage(testResult, errorMessage); } } return !possibleOutOfMemoryError; }
private static boolean possibleOutOfMemory(int neededMemory) { Runtime runtime = Runtime.getRuntime(); long maxMemory = runtime.maxMemory(); long realFreeMemory = runtime.freeMemory() + (maxMemory - runtime.totalMemory()); long meg = 1024 * 1024; long needed = neededMemory * meg; return realFreeMemory < needed; }
// [myakovlev] Do not delete - it is for debugging public static void tryGc(int times) { if ((ourMode & RUN_GC) == 0) return; for (int qqq = 1; qqq < times; qqq++) { try { Thread.sleep(qqq * 1000); } catch (InterruptedException e) { e.printStackTrace(); } System.gc(); // long mem = Runtime.getRuntime().totalMemory(); log("Runtime.getRuntime().totalMemory() = " + Runtime.getRuntime().totalMemory()); } }