1 21 22 package org.apache.derby.iapi.services.memory; 23 24 50 public class LowMemory { 51 52 62 private long lowMemory; 63 64 68 private long whenLowMemorySet; 69 70 78 public void setLowMemory() { 79 80 if (lowMemory == 0L) { 83 84 for (int i = 0; i < 5; i++) { 94 System.gc(); 95 System.runFinalization(); 96 try { 97 Thread.sleep(50L); 98 } catch (InterruptedException e) { 99 } 100 } 101 } 102 synchronized (this) { 103 if (lowMemory == 0L) { 104 lowMemory = Runtime.getRuntime().freeMemory(); 105 whenLowMemorySet = System.currentTimeMillis(); 106 } 107 } 108 } 109 110 114 public boolean isLowMemory() { 115 synchronized (this) { 116 long lm = lowMemory; 117 if (lm == 0) 118 return false; 119 120 if (Runtime.getRuntime().freeMemory() > lm) 121 return false; 122 123 129 long now = System.currentTimeMillis(); 130 if ((now - this.whenLowMemorySet) > 5000L) { 131 lowMemory = 0L; 132 whenLowMemorySet = 0L; 133 return false; 134 } 135 return true; 136 } 137 } 138 } 139 | Popular Tags |