3

ProcessBuilder.start and Runtime.exec seem to use fork() on *NIX system, which seems to allocate the child process the same amount of memory as the parent process (see e.g. this question ). This can be painful if you want to launch a process which needs almost no memory from a process that uses lots of memory.

Is there any way to launch processes using POSIX_SPAWN -- which doesn't do this memory allocation? The only way I know of is using Tanuki, but turns out to be not ideal for me.

Community
  • 1
  • 1
Jacob
  • 1,671
  • 4
  • 23
  • 26
  • Want to elaborate on "turns out to be not ideal"? – Matthew Flaschen Jun 25 '10 at 03:06
  • I'm using an earlier version of Tanuki that doesn't support the feature and can't upgrade my dependency for stability reasons. Also, It would be nice to be working with java.lang.Process instead of WrapperProcess or whatnot. – Jacob Jun 25 '10 at 03:10
  • I recall that forked processes LOOKED like they were using the same amount of memory as the parent (in the process list on linux/unix), but in fact they were all SHARING the memory. You could test this empirically - does it run your machine out of memory, or look up details of the ps tool (assuming that's what you're using). – Paul Jowett Jun 25 '10 at 07:55
  • I'm having the exact same issue and would love a cheap/easy answer. Anybody? – Morinar Jul 13 '10 at 22:56

3 Answers3

1

Use the NuProcess library. It uses VFORK on Linux which does not copy the process space first, thereby eliminating the frequent OOM errors when forking processes from a java process with a large heap.

brettw
  • 10,664
  • 2
  • 42
  • 59
1

Here's an open source project which implements posix_spawn

https://github.com/axiak/java_posix_spawn

tommy chheng
  • 9,108
  • 9
  • 55
  • 72
0

Since Java 13+ ProcessBuilder.start uses posix_spawn. See: https://bugs.openjdk.org/browse/JDK-8213192.

Jasper Siepkes
  • 1,412
  • 16
  • 25