A Java application cannot control the amount of memory or (or CPU) used by its threads, whether the threads are trusted or untrusted code. There is no API to do this in existing generations of JVMs. And, of course, there are no APIs to monitor the use of threads in memory. (Itβs not even clear that this is a meaningful concept ...)
The only way to guarantee the use of resources of untrusted Java code is to run the code in a separate JVM and use operating system level resource controls (such as ulimit, nice, sigstop, etc.) and "-Xmx" to limit the use of JVM resources.
Some time ago, Sun produced the JSR 121 , which aimed to solve this problem. This JSR will allow you to divide the application into parts (called "isolates"), which are transmitted through messaging, and offered the ability to isolate one to control and manage the other. Unfortunately, Isolate APIs are not yet implemented in any core JVM.
source share