When java jvm compiles bytecode, where does that code go into process space?

When jvm (hotspot in my case) constantly compiles certain code codes into machine code, where is that machine code stored? in the process text memory segment? in the process of heap

<y> I'm not talking about jiting. From my understanding, JIT will compile and run bytecode without saving the compiled code anywhere. But what about when jvm saves this code - where in the process space does it save it? ... as the comments and answers note, everything I asked for is actually part of the JIT.

EDIT:

according to my comment below, the situation that I specifically mention is described here as โ€œAdaptive Optimizationโ€: http://www.oracle.com/technetwork/java/whitepaper-135217.html#hotspot

+8
java memory process jvm jvm-hotspot
source share
2 answers

First, what you describe is JIT - in particular, how it works in Hotspot

To answer the question of where the code is stored at runtime, it is on the process heap and the pointers to the method code in the Klass File object are updated to point to it. There's also something called OSR (when replacing the stack) to compile long loop cycles directly on the stack.

+3
source share

I did not work with production-quality virtual machines, but here are my five cents.

.text sections in Unix executables refer to the file in which executable code is stored; at runtime, this section of the file is mapped to the memory area allocated by the system linker during program initialization, which is all (on Linux, you can see the partition markup in memory in /proc/$PID/maps ).

Regarding JIT compilation on Unix-like systems, I can only think of the memory areas that were allocated by mmap system call with the PROT_EXEC flag. This call is defined by POSIX standards and is used by the Linux system linker ld.so to load any native executable into memory. This call can equally be used to allocate new areas of executable memory at run time.

The regular heap is often protected by the OS / MMU from execution, as any /proc/$PID/maps file shows:

 00dd4000-01292000 rw-p 00000000 00:00 0 [heap] 

here rw-p means that no data in [heap] can be executed (although, for example, this does not apply to x86 32-bit processors without PAE, they do not have hardware capabilities to prevent some memory data from running as code), but can be read / written.

Thus, the virtual machine requires a dedicated memory area with permission to execute code. In fact, let's look at the rwx memory areas in some Java memory layouts:

 # cat /proc/12929/maps | grep rwx # I run a Java VM with PID 12929 f3700000-f3940000 rwxp 00000000 00:00 0 # - an unnamed executable & writable region 

Then executing native code is a matter of building a JIT-compiled native code, regardless of position (for example, shared object code, with gcc option -fPIC ) or using the address returned by mmap() .

+3
source share

All Articles