Limit resource utilization of JNA calls without changing dll

94 Views Asked by At

How can you prevent a JNA method-call from exceeding thresholds for CPU utilization, thread-counts, and memory limits?

Background:

I'm working on a safety critical application and one of the non-safety-critical features requires the use of a library written in C. The dlls have been given to me as a black-box and there's no chance that I'll get access to the source code beyond the java interface files. Is there a way to limit the CPU usage, thread-count, and memory used by the JNA code?

2

There are 2 best solutions below

0
On

See ulimit and sysctl, which are applicable to your overall JVM process (or any other process, for that matter).

It's not readily possible to segment parts of your JVM which are making native accesses via JNA from those that aren't, though.

You should run some profiling while you exercise your shared library to figure out what resources it does use, so you can focus on setting limits around those (lsof or strace would be used on linux, I'm not sure of the equivalent on windows).

0
On

For most operating systems you must either call your C code from a new thread or new process. I would recommend calling it from a new process as then you can sandbox it easier and deeper. Typically on a Unix like system one switches to a new user set aside for the service and that has user resource limits on it. However, on Linux one can use user namespaces and cgroups for more dynamic and flexible sandboxing. On Microsoft Windows one typically uses Job objects for resource sandboxing but permissions based sandboxing is more complicated (a lot of Windows is easily sandboxable with access controls but the GUI and window messaging parts make things complicated and annoying).