There is an example to avoid scripts running more than 10sec. You can use MBean 
to monitor heap space and if out of memory is near you throw an error. With 
your example the heap can grow pretty fast so you need to make Rhino runtime to 
call observeInstructionCount more often than once for each 10000 bytecode 
instructions. You can also take a look at this: 
http://www.javaspecialists.co.za/archive/Issue092.html for OutOfMemoryError 
Warning System.

Infinite loop is easier to detect if memory usage does not increase so much. 
Given that you use MBean to monitor memory usage then, if several scripts are 
executed then it is not guaranteed that the one causing the leak is stopped. 
Memory consumption has to be monitored on thread level but I'm not sure that's 
possible.

-----Original Message-----
From: 
dev-tech-js-engine-rhino-bounces+priit.liivak=webmedia...@lists.mozilla.org 
[mailto:dev-tech-js-engine-rhino-bounces+priit.liivak=webmedia...@lists.mozilla.org]
 On Behalf Of Rhino user
Sent: Wednesday, February 04, 2009 9:59 PM
To: [email protected]
Subject: how to avoid out of memory with infinite loop

My script looks like this -
var i = "0";

init("A qucik brown fox jumps over the lazy dog A qucik brown fox
jumps over the lazy dog A qucik brown fox jumps over the lazy dog A
qucik brown fox jumps over the lazy dog A qucik brown fox jumps over
the lazy dog A qucik brown fox jumps over the lazy dog A qucik brown
fox jumps over the lazy dog A qucik brown fox jumps over the lazy dog
A qucik brown fox jumps over the lazy dog A qucik brown fox jumps over
the lazy dog A qucik brown fox jumps over the lazy dog A qucik brown
fox jumps over the lazy dog A qucik brown fox jumps over the lazy dog
A qucik brown fox jumps over the lazy dog A qucik brown fox jumps over
the lazy dog A qucik brown fox jumps over the lazy dog A qucik brown
A qucik brown fox jumps over the lazy dog A qucik brown fox jumps over
the lazy dog A qucik brown fox jumps over the lazy dog A qucik brown
fox jumps over the lazy dog A qucik brown fox jumps over the lazy dog
A qucik brown fox jumps over the lazy dog A qucik brown fox jumps over
the lazy dog A qucik brown fox jumps over the lazy dog A qucik brown
fox jumps over the lazy dog A qucik brown fox jumps over the lazy dog
A qucik brown fox jumps over the lazy dog A qucik brown fox jumps over
the lazy dog A qucik brown fox jumps over the lazy dog ");

function init(s)
{
    while(i=="0")
        s+=s;
}

I get
ava.lang.OutOfMemoryError: Java heap space
        at java.lang.String.concat(String.java:1831)
        at org.mozilla.javascript.ScriptRuntime.add(ScriptRuntime.java:2296)
        at org.mozilla.javascript.gen.c3._c1(externalScript:11)
        at org.mozilla.javascript.gen.c3.call(externalScript)

How can I avoid it?
Thanks
-Anupama
_______________________________________________
dev-tech-js-engine-rhino mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino
_______________________________________________
dev-tech-js-engine-rhino mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino

Reply via email to