#231 BeanShell's handling of java.lang.OutOfMemoryError

open
nobody
None
5
2005-07-13
2005-07-13
Nigel
No

>cat t91.java
import java.util.*;

public class t91 {
private Hashtable data = new Hashtable();

public Object get(String key) {
Object obj = data.get(key);
if (obj == null) {
System.out.print(key
+ " ");
try {
obj = new
Double[1000000];
data.put
(key, obj);
} catch
(OutOfMemoryError e) {

System.out.print("\nNigel ");
flushCache
();
obj = get
(key);
}
}
return obj;
}

public void flushCache() {
System.out.println("is best");
data.clear();
}

public static void main(String[] args) {
t91 t91 = new t91();
int count = 0;
while (count < 100)
t91.get("" + count++);
System.out.println("Enough is
enough");
}
}

Process cat exited with code 0

Using Java produces my preferred result until I become
bored:

> javac t91.java
Note: t91.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Process javac exited with code 0

> java t91
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Nigel is best
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
Nigel is best
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
Nigel is best
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
Nigel is best
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
Nigel is best
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
Nigel is best
96 97 98 99 Enough is enough
Process java exited with code 0

but BeanShell has other ideas for OutOfMemoryError.

> java -classpath ../../bsh-2.0b5.jar bsh.Interpreter
t91.java
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Class: class
t91 main method threw exception:Method Invocation
t91.get : at Line: 34 : in file: t91.java : t91 .get ( "" +
count ++ )

Called from method: main : at Line: -1 : in file: <Called
from Java Code> : <Compiled Java Code>
Target exception: java.lang.OutOfMemoryError: Java
heap space

Process java exited with code 0

Discussion


Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks