source: src/test/java/agents/nastyagent/NearlyOutOfMem.java@ 127

Last change on this file since 127 was 1, checked in by Wouter Pasman, 6 years ago

Initial import : Genius 9.0.0

File size: 916 bytes
Line 
1package agents.nastyagent;
2
3import java.util.ArrayList;
4import java.util.List;
5
6import genius.core.actions.Action;
7
8/**
9 * Deliberately goes out of memory while choosing an action.
10 *
11 * @author W.Pasman 25jan16
12 *
13 */
14public class NearlyOutOfMem extends NastyAgent {
15 private ArrayList<String> memory = new ArrayList<String>();
16 private final static String block = "01234567890123456789123456789012";
17
18 @Override
19 public Action chooseAction(List<Class<? extends Action>> possibleActions) {
20 doubleMemory();
21 return super.chooseAction(possibleActions);
22 }
23
24 /**
25 * Double the amount of used memory (starts with 32 bytes if no mem in use
26 * yet) until only 1000 bytes are left.
27 */
28 private void doubleMemory() {
29 long toAdd = memory.size();
30 if (toAdd == 0) {
31 toAdd = 1;
32 }
33 for (long i = 0; i < toAdd; i++) {
34 memory.add(block);
35 if (Runtime.getRuntime().freeMemory() < 10000)
36 return;
37 }
38 }
39}
Note: See TracBrowser for help on using the repository browser.