Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

example for detecting-low-memory-in-java-part-2

  • Loading branch information...
commit feee6d0dc0327753a3a702cd61867258346b86a3 1 parent c66881b
@julienledem authored
View
98 2011/07/21/detecting-low-memory-in-java-part-2/MemoryTest.java
@@ -0,0 +1,98 @@
+import java.lang.management.ManagementFactory;
+import java.lang.management.MemoryMXBean;
+import java.lang.management.MemoryNotificationInfo;
+import java.lang.management.MemoryPoolMXBean;
+import java.lang.management.MemoryType;
+import java.lang.management.MemoryUsage;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.management.Notification;
+import javax.management.NotificationEmitter;
+import javax.management.NotificationListener;
+
+public class MemoryTest {
+
+ static boolean stop = false;
+ static MemoryPoolMXBean tenuredGenPool = null;
+
+ public static void main(String[] args) {
+ for (MemoryPoolMXBean pool :
+ ManagementFactory.getMemoryPoolMXBeans()) {
+ // see http://www.javaspecialists.eu/archive/Issue092.html
+ if (pool.getType() == MemoryType.HEAP && pool.isUsageThresholdSupported()) {
+ tenuredGenPool = pool;
+ }
+ }
+
+ tenuredGenPool.setCollectionUsageThreshold((int)Math.floor(tenuredGenPool.getUsage().getMax()*0.8));
+ tenuredGenPool.setUsageThreshold((int)Math.floor(tenuredGenPool.getUsage().getMax()*0.8));
+
+ MemoryMXBean mbean = ManagementFactory.getMemoryMXBean();
+ NotificationEmitter emitter = (NotificationEmitter) mbean;
+ emitter.addNotificationListener(new NotificationListener() {
+ public void handleNotification(Notification n, Object hb) {
+ if (n.getType().equals(
+ MemoryNotificationInfo.MEMORY_COLLECTION_THRESHOLD_EXCEEDED)) {
+
+ System.err.println("memory collection threshold exceeded !!! : \n "+memString());
+ stop=true;
+
+ } else if (n.getType().equals(
+ MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED)) {
+ System.out.println("memory threshold exceeded !!! : \n "+memString());
+ }
+ }
+ }, null, null);
+
+ StringBuffer sb = new StringBuffer();
+ for (int i = 0; i < 1000; i++) {
+ sb.append("all work and no play... makes this a long string\n");
+ }
+
+ long t0 = 0;
+ int capacity = 1000000;
+ List<String> strings = new ArrayList<String>(capacity);
+ for (int i = 0; i < capacity && !stop; i++) {
+ strings.add("randomString:"+Math.random()+sb);
+ for (int j = 0; j < 10; j++) {
+ strings.set((int)Math.floor(Math.random()*strings.size()), "randomString:"+Math.random()+sb);
+ }
+ if (i%100 == 0) {
+ long t1 = System.currentTimeMillis();
+ if (t1-t0 > 100) {
+ t0 = t1;
+ System.out.println("it=" + pan(String.valueOf(i), 4) + memString());
+ }
+ }
+ }
+
+ }
+
+ private static String pan(String str, int i) {
+ while (str.length()<i) {
+ str = " "+str;
+ }
+ return str;
+ }
+
+ private static String memString() {
+ StringBuffer sb = new StringBuffer();
+ for (MemoryPoolMXBean pool :
+ ManagementFactory.getMemoryPoolMXBeans()) {
+ if (pool.getType().equals(MemoryType.HEAP)) {
+ sb.append(" - ")
+ .append(pool.getName())
+ .append(": u=").append(toString(pool.getUsage()))
+ .append(" cu=").append(toString(pool.getCollectionUsage()))
+ .append(" th=").append(pool.getCollectionUsageThreshold()*100/pool.getUsage().getMax()+"%");
+ }
+ }
+ return sb.toString();
+ }
+
+ private static String toString(MemoryUsage memoryUsage) {
+ String string = memoryUsage.getUsed()*100/memoryUsage.getMax()+"%";
+ return pan(string, 4);
+ }
+}
View
32 2011/07/21/detecting-low-memory-in-java-part-2/out.txt
@@ -0,0 +1,32 @@
+it= 0 - Par Eden Space: u= 49% cu= 0% th=0% - Par Survivor Space: u= 0% cu= 0% th=0% - CMS Old Gen: u= 0% cu= 0% th=79%
+it= 100 - Par Eden Space: u= 16% cu= 0% th=0% - Par Survivor Space: u= 99% cu= 99% th=0% - CMS Old Gen: u= 36% cu= 0% th=79%
+it= 200 - Par Eden Space: u= 86% cu= 0% th=0% - Par Survivor Space: u= 95% cu= 95% th=0% - CMS Old Gen: u= 24% cu= 21% th=79%
+memory threshold exceeded !!! :
+ - Par Eden Space: u= 9% cu= 0% th=0% - Par Survivor Space: u= 95% cu= 95% th=0% - CMS Old Gen: u= 32% cu= 21% th=79%
+memory threshold exceeded !!! :
+ - Par Eden Space: u= 0% cu= 0% th=0% - Par Survivor Space: u= 99% cu= 99% th=0% - CMS Old Gen: u= 83% cu= 21% th=79%
+it= 300 - Par Eden Space: u= 50% cu= 0% th=0% - Par Survivor Space: u= 99% cu= 99% th=0% - CMS Old Gen: u= 33% cu= 30% th=79%
+memory threshold exceeded !!! :
+ - Par Eden Space: u= 0% cu= 0% th=0% - Par Survivor Space: u= 99% cu= 99% th=0% - CMS Old Gen: u= 83% cu= 30% th=79%
+it= 400 - Par Eden Space: u= 15% cu= 0% th=0% - Par Survivor Space: u= 99% cu= 99% th=0% - CMS Old Gen: u= 51% cu= 51% th=79%
+memory threshold exceeded !!! :
+ - Par Eden Space: u= 0% cu= 0% th=0% - Par Survivor Space: u= 99% cu= 99% th=0% - CMS Old Gen: u= 80% cu= 51% th=79%
+it= 500 - Par Eden Space: u= 79% cu= 0% th=0% - Par Survivor Space: u= 99% cu= 99% th=0% - CMS Old Gen: u= 75% cu= 46% th=79%
+memory threshold exceeded !!! :
+ - Par Eden Space: u= 0% cu= 0% th=0% - Par Survivor Space: u= 99% cu= 99% th=0% - CMS Old Gen: u= 80% cu= 46% th=79%
+memory threshold exceeded !!! :
+ - Par Eden Space: u= 0% cu= 0% th=0% - Par Survivor Space: u= 99% cu= 99% th=0% - CMS Old Gen: u= 82% cu= 50% th=79%
+it= 600 - Par Eden Space: u= 43% cu= 0% th=0% - Par Survivor Space: u= 99% cu= 99% th=0% - CMS Old Gen: u= 68% cu= 55% th=79%
+memory threshold exceeded !!! :
+ - Par Eden Space: u= 0% cu= 0% th=0% - Par Survivor Space: u= 99% cu= 99% th=0% - CMS Old Gen: u= 83% cu= 55% th=79%
+memory threshold exceeded !!! :
+ - Par Eden Space: u= 0% cu= 0% th=0% - Par Survivor Space: u= 99% cu= 99% th=0% - CMS Old Gen: u= 80% cu= 71% th=79%
+it= 700 - Par Eden Space: u= 8% cu= 0% th=0% - Par Survivor Space: u= 99% cu= 99% th=0% - CMS Old Gen: u= 78% cu= 64% th=79%
+memory threshold exceeded !!! :
+ - Par Eden Space: u= 0% cu= 0% th=0% - Par Survivor Space: u= 99% cu= 99% th=0% - CMS Old Gen: u= 83% cu= 64% th=79%
+memory threshold exceeded !!! :
+ - Par Eden Space: u= 0% cu= 0% th=0% - Par Survivor Space: u= 99% cu= 99% th=0% - CMS Old Gen: u= 82% cu= 67% th=79%
+memory threshold exceeded !!! :
+ - Par Eden Space: u= 0% cu= 0% th=0% - Par Survivor Space: u= 99% cu= 99% th=0% - CMS Old Gen: u= 83% cu= 70% th=79%
+memory collection threshold exceeded !!! :
+ - Par Eden Space: u= 15% cu= 15% th=0% - Par Survivor Space: u= 99% cu= 99% th=0% - CMS Old Gen: u= 84% cu= 84% th=79%
Please sign in to comment.
Something went wrong with that request. Please try again.