The difference between the runtime data area of Java and JVM memory model

First of all, the two are completely different concepts, it must not be confused.

1. What is Java memory model?

Java memory model is shared write variables for Java language (actually manipulated variable corresponding to the shared memory) specification in the case of multi-threaded, multi-threaded mainly to solve the visibility, atomicity problem solving shared variable multithreaded operating conflict.

Common multithreaded programming is:

  • The resulting non-see
  • The naked eye can not detect the accuracy of the program
  • Different operating platforms of different performance
  • Error is difficult to reproduce

So JVM specification defines a Java virtual machine for some of the rules of multi-threaded memory operations, mainly reflected in the volatile and synchronized two keywords.

  • volatile is to ensure visibility at the time of multi-threaded read and write to shared variables provided by the JVM, the main role is volatile modification of shared variables are cached ban (related here with the CPU cache and cache coherency protocol), do not do reordering (reordering: optimization of the processing in the CPU status much faster than memory read and write speeds performed to improve performance), but does not guarantee the atomicity operation shared variables.
  • JVM synchronized locking mechanism is provided by atomic memory barrier properties to ensure that the lock and the lock region of operation, visibility, orderly.
  • Lock contention is the object (static class object is locked, the lock is the current non-static objects, i.e. the this, the lock block lock method is custom objects) in the heap memory of a memory object header "Sovereign", only the thread can acquire a "sovereign", i.e. exclusive, atomic operations to ensure exclusive locked by the lock region
  • By addition of the barrier before and after loading the code (Load Barrier) and a storage barrier (Store Barrier), to ensure the visibility of the locking block or a method of operating the shared variables
  • Obtaining by the addition of a barrier (Acquire Barrier) and a release barrier (Release Barrier) before and after the code, to ensure orderly method to lock or block the operation of the shared variables

2. What is the JVM run-time data area?

JVM runtime data area is divided into a logical Java virtual machine running the Java process memory undertaken, including methods district, heap memory, virtual machine stack, native method stacks, the program counter. These blocks are actually different Java process to apply to the use of memory by different data structures in the operation of the Java Virtual Machine.

  • Method Area: JVM loads class information for storing, constants, static variables, the compiled code and other data. Different virtual machines have different implementations, oracle of HotSpot on behalf permanently in the method area Java7, Java8 methods zone on element space, and are managed by GC mechanism.
  • VM stack: private to each thread of a space, a plurality of stack frames, a method corresponding to a stack frame, the stack frame includes a local variable table, the operand stack, dynamic linking, return address method, the additional information and the like. The default maximum stack memory 1M, ran beyond StackOverFlowError.
  • Native method stacks: stack similar to the virtual machine, the virtual machine is to use native and native methods of preparation. Concrete realization is achieved by the virtual machine manufacturers. HotSpot virtual machine to achieve consistent virtual machine stack, while exceeding the size of the cast StackOverFlowError.
  • Program Counter: the recording position of the current thread of execution, bytecode, the bytecode instruction is stored in the address, if the native method is null. CPU can execute only one thread at the same time in the instruction, the thread switch is performed to restore the correct position by the program counter.
  • Heap memory: all threads can access the modified object instance is stored, is the largest part of the space occupied by the data area is divided into the old and the new generation of years in the HotSpot virtual machine, the new generation is divided into Eden area and Survivor0 area, Survivor1 area.

Guess you like

Origin blog.51cto.com/14230003/2443407