With the -Xms and -Xmx Options, it is possible to set the initial and maximum size of the memory allocation pool. Using strace/truss on Linux and AIX I found out, the JVM internally uses the (k)mmap system call. The address-Parameter is NULL, therefore the operating system decides, at which virtual memory address it maps the memory.
$ truss java -Xmx512M Hello 2>&1 | grep mmap
kmmap(0x00000000, 536870912, 3, 17, -1, 0x00000000, 0x00000000) = 0xB0000000
Is is possible, to specify this address?
Background: I have to call legacy code via the Java Native Interface (JNI), which requires huge amounts of non-relocateable data (2 GB in a 32 bit address space) mapped at a specific location in memory. This region overlaps with the location of Javas memory allocation pool.
Edit: This is the actual memory layout:
0x0... AIX
0x1... Text
0x2... Stack
0x3... Heap
0x4... Heap
...... Legacy Data (2 GB)
0xd... Shared Library Text
0xe... unused
0xf... Shared Library Data
My goal is to move the Java memory allocation pool from 0xb/0xc into the 0x3/0x4 segments, which is also available in the standard (non large) memory model.
Perhaps you could turn this problem inside-out. Start a native process, and do what you need to do to reserve your non-negotiable space (mmap or whatever). Then use Java’s invocation API to create and start a JVM inside your native process. I can’t find any documentation about how that JVM does its memory management, but it stands to reason that it will play nicely with memory that has already been allocated by the host process (i would guess it just uses the local malloc), so it will find somewhere else to put its heap. If you leave the 0x3-0x4 region unallocated, you can hope it will put it there.
However, i think there’s a real risk that there just isn’t enough address space left in your memory model for a JVM. The 0x3-0x4 region is, what, 512 MB? If you can fit a whole JVM (heap, stack, perm, VM structures, etc) in there, then fine, but if not, you may find that when the JVM can’t allocate contiguous memory for the heap, it gets very upset. Or it may not. I really don’t know.
So, having suggested this, i would strongly recommend that you don’t do it, and instead follow sleske’s advice and put the native code in a separate process.