On Feb 23, 7:49 pm, Rhino user <[email protected]> wrote:
> On Feb 5, 11:42 pm, Priit Liivak <[email protected]> wrote:
>
>
>
> > 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.htmlforOutOfMemoryError
> >  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
> > _______________________________________________
> > dev-tech-js-engine-rhino mailing list
> > [email protected]https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino
>
> I implemented context and contextfactory. And tried to play around
> with the instruction  count and stack depth . I also adjusted the
> time. This script quickly eats up  memory. Only if the time is
> adjusted to like 100 ms  then it throws error rather than oom.
> here is my code , pl. let me know what more can I do. Next  I am going
> to try the MxBeans. before that , i want to know if anything with
> Rhino can do it.
> public class MyFactory extends ContextFactory
> {
>
>     // Custom Context to store execution time.
>     private static class MyContext extends Context
>     {
>         long startTime;
>
>         MyContext(ContextFactory f ){
>                 super(f);
>         }
>         protected void observeInstructionCount(Context cx, int
> instructionCount)
>         {
>
>                 MyFactory f = (MyFactory) getFactory();
>            f.observeInstructionCount(this, instructionCount);
>             System.out.println("instructionCount in mycontext "+
> instructionCount);
>             long currentTime = System.currentTimeMillis();
>             if (currentTime - startTime > .1*10) {
>                 // More then 10 seconds from Context creation time:
>                 // it is time to stop the script.
>                 // Throw Error instance to ensure that script will
> never
>                 // get control back through catch or finally.
>                 throw new java.lang.ThreadDeath();
>                 //return;
>             }
>         }
>     }
>
>     static {
>         // Initialize GlobalFactory with custom factory
>         ContextFactory.initGlobal(new MyFactory());
>     }
>
>     // Override makeContext()
>     protected Context makeContext()
>     {
>         MyContext cx = new MyContext(this);
>         cx.setClassShutter(new ClassShutter() {
>                         public boolean visibleToScripts(String className) {
>                                 return 
> className.startsWith("org.mozilla.javascript") ||
> className.startsWith( "java.lang.Object");
>                         }
>                 });
>         if(controller != null)
>                 cx.setSecurityController(controller);
>         System.out.println("setOptimizationLevel "+
> cx.getOptimizationLevel());
>         cx.setOptimizationLevel(-1);
>         cx.setMaximumInterpreterStackDepth(1);
>         // Make Rhino runtime to call observeInstructionCount
>         // each 10000 bytecode instructions
>         System.out.println("getMaximumInterpreterStackDepth "+
> cx.getMaximumInterpreterStackDepth());
>         cx.setInstructionObserverThreshold(10);
>
>         return cx;
>     }
>     JSSecurityController controller = null;
>     public void setSecurityController(JSSecurityController
> SecController){
>         controller = SecController;
>     }
>     // Override hasFeature(Context, int)
>     public boolean hasFeature(Context cx, int featureIndex)
>     {
>         // Turn on maximum compatibility with MSIE scripts
>         switch (featureIndex) {
>             case Context.FEATURE_NON_ECMA_GET_YEAR:
>                 return true;
>
>             case Context.FEATURE_MEMBER_EXPR_AS_FUNCTION_NAME:
>                 return true;
>
>             case Context.FEATURE_RESERVED_KEYWORD_AS_IDENTIFIER:
>                 return true;
>
>             case Context.FEATURE_PARENT_PROTO_PROPRTIES:
>                 return false;
>         }
>         return super.hasFeature(cx, featureIndex);
>     }
>
>     // Override observeInstructionCount(Context, int)
>     protected void observeInstructionCount(Context cx, int
> instructionCount)
>     {
>
>         MyContext mcx = (MyContext)cx;
>         ContextFactory f = mcx.getFactory();
>         //f.observeInstructionCount(mcx, instructionCount);
>        // System.out.println("instructionCount "+ instructionCount);
>         long currentTime = System.currentTimeMillis();
>         if (currentTime - mcx.startTime > 5*1000) {
>             // More then 10 seconds from Context creation time:
>             // it is time to stop the script.
>             // Throw Error instance to ensure that script will never
>             // get control back through catch or finally.
>                 throw new java.lang.ThreadDeath();
>                 //return;
>         }
>     }
>
>     // Override doTopCall(Callable, Context, Scriptable,Scriptable,
> Object[])
>     protected Object doTopCall(Callable callable,
>                                Context cx, Scriptable scope,
>                                Scriptable thisObj, Object[] args)
>     {
>         MyContext mcx = (MyContext)cx;
>         mcx.startTime = System.currentTimeMillis();
>
>         return super.doTopCall(callable, cx, scope, thisObj, args);
>     }
>
> }

Any ideas? solutions?
_______________________________________________
dev-tech-js-engine-rhino mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino

Reply via email to