
org.springframework.block.MemoryBlock Maven / Gradle / Ivy
package org.springframework.block;
import java.util.ArrayList;
import java.util.List;
public class MemoryBlock extends AbstractBlock {
public MemoryBlock() {
this(0);
}
public MemoryBlock(long timeout) {
super(timeout);
}
@Override
public void eval() {
long memory = Runtime.getRuntime().freeMemory();
logger.info("TroubleMaker client: Memory requested.");
logger.info("Eating Memory - started at: " + memory);
List buffer = new ArrayList();
while (true) {
try {
char[] c = new char[Integer.MAX_VALUE / 4];
for (int i = 0; i < Integer.MAX_VALUE / 4; i++) {
c[i] = (char) i;
if (i % 1000000 == 0) {
logger.info("Memory Eaten -" + (Runtime.getRuntime().freeMemory() - memory));
}
buffer.add(c);
}
}
catch (OutOfMemoryError e) {
long start = System.currentTimeMillis();
String msg = timeout == 0 ? "NEVER" : "" + (timeout / 60000) + " minute(s)";
logger.info("Heap MEMORY limit reached...will stay in low memory condition for " + msg);
while (true) {
try {
long sleep = 1000;
if (System.currentTimeMillis() - start >= timeout && timeout > 0) {
buffer = null;
break;
}
Thread.sleep(sleep);
}
catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
buffer = null;
logger.info("Memory Consumption stopped");
Thread.currentThread().stop();
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy