I use mozila rhino to provides user JavaScript customization capabilities. But if user create a large object in JS, it will cause jvm oom error. For performance reasons, I don't want to put user JS run in a separate jvm process. So I want to find a way to restrict memory usage of jvm thread.
// user js like this
test();
function test(){
var arr = [];
while(true){
arr.push("test string");
}
}
PS: Rhino is a JS execution engine written in java. It will compile js into java and run it in the jvm process.
Informations I have found so far:
- There are many ways to calculate Java object memory usage, like ehcache sizeof. But it can only calculate memory of a java object, not work in this scenario. Because the memory is allocated within a method.
- I found a sandbox for rhino, but it can only restrict cpu usage.
Maybe if there are anyway to calculate memory usage during thread running, I can block it during user code execution. Can anyone give some advice or ideas?