Limit memory and time

Jun 25, 2011 at 9:35 PM

I'd like to use Jint to provide flexibility to a C# application, where a number of independent scripts would be used to customize the behavior of the software.  Each engine is running on a different thread.  So far this is working great.  But I need to ensure that the application behaves itself, including any scripts it executes through Jint.  I need the ability to detect (and stop) any script that's eating up too much memory or taking too long to execute.

Is there some way I can set limits on the amount of memory and execution time that a script can take?  I've set the recursion limit, but I haven't found a way to limit the total number of bytes allocated in the script or how to set a maximum execution time.  Is there any way to do this?  A hard limit for each would be ideal.

I've briefly looked into using an "overseer" thread on a timer to monitor the Jint engines, and kill a specific engine if it's script has exceeded either limit.  The time limit is certainly easy to enforce.  Unfortunately, there appears to be no reliable or fast way in .NET to get the size in bytes of an object and it members (such as a Jint engine).  The best idea I have is to roughly estimate the size by counting the number of local and global variables, but this would only be approximate and could be affected by thread issues.  So I'm hoping to find a cleaner, more reliable solution.


Jun 26, 2011 at 6:40 PM

I don't think we can handle it at Jint's level. Though you might want to ask the question on, trying to find out how to monitor any thread's memory ?