Java Memory and data area

Source: Internet
Author: User
Tags volatile

The data area of the Java Runtime includes: (where the first two are thread-shared)
    • 1. Method area stores data such as class information, constants, static variables, instant compiler compiled code, etc. loaded by the virtual machine

    • 2. Heap holds object instances where almost all object instances allocate memory

    • 3. The virtual machine stack (VM stack) describes the memory model that is executed by the Java method: Each method executes while creating a Stack Frame (the underlying data structure of the method runtime) to store information such as local variable tables, operand stacks, dynamic joins, method exits, and so on

    • 4. The local method stack (Native) is similar to the virtual machine stack, but the Native method service used by the virtual machine. (Some virtual machines, such as sun hotspot virtual machines, directly merge the local method stack with the virtual machine stack)

    • 5. Program Counter Register can be regarded as the identifier of the line number of the byte code executed by the current thread

The total JVM memory is divided into 4 parts:
1. Stack segment
2. Heap Segment
3. Code Segment
4. Data segment)
    • When we declare a local variable in the program, this variable is stored in the stack segment (stack);

    • This object is placed in the heap segment when new object is set;

    • Static variables or string constants exist in the data segment.

    • The methods in the class are present in code segment.

The memory of a Java Virtual machine can be divided into three regions:
  • Stack stack

    • Characteristics:
      • (1), the stack describes the method execution of the memory model, each method is called to create a stack frame (store local variables, operands, method entry, etc.);
      • (2), the JVM creates a stack for each thread that holds information about the execution method of the thread (actual parameters, local variables, etc.);
      • (3), the stack belongs to the thread private, can not realize the sharing between threads;
      • (4), the storage characteristics of the stack is advanced, after-first out ;
      • (5), the stack is automatically allocated by the system, the speed is fast, the stack is a continuous memory space.
  • Heaps heap

    • Characteristics:
      • (1), the heap is used to store the created objects and arrays (arrays are objects);
      • (2), the JVM has only one heap, is shared by all threads;
      • (3), Heap is a discontinuous memory space, the allocation of flexible, slow speed.
  • Method Area (in heap)

    • Characteristics:
      • (1), the JVM has only one method area, is shared by all threads;
      • (2), the method area is actually a heap, only for storing classes, constants related information;
      • (3), the use of the storage program is always the same or unique content. (class Information "Class object", static variable, string constant, etc.)
Java Runtime Data area
    • Runtime data area includes: Virtual machine stack, heap area, method area, local method stack, program counter

      • Virtual Machine Stack Area: that is, we often say that the stack area, thread-private, storage base type, object reference and ReturnAddress, during compilation to complete the allocation.

      • Heap area, Java heap, also known as GC heap, all threads shared, storage instances and arrays of objects, Java heap is the main area of garbage collector management.

      • Method Area: All threads are shared, storing data such as class information, constants, static variables, and instant compiler compiled code that have been loaded by the virtual machine. The target of memory reclamation in this area is mainly the collection of objects for the constant pool and the unloading of the types.

      • Program counter: Thread private, each thread has its own independent program counter, used to indicate the address of the next instruction.

Java memory model
    • Communication between Java threads is controlled by the Java Memory Model (JMM), which JMM determines when a thread's write to a shared variable is visible to another thread.

      • From an abstract point of view, JMM defines the abstract relationship between threads and main memory: Shared variables between threads are stored in main memory, and each thread has a private local memory (local memory) in which the thread is stored in a copy of the read/write shared variable. Local memory is an abstract concept of JMM and is not really there. It covers caching, write buffers, registers, and other hardware and compiler optimizations
      • The write-read of volatile variables allows for communication between threads.
      • In terms of memory semantics, volatile has the same effect as a monitor lock: volatile writes and the release of the monitor have the same memory semantics, and volatile reads have the same memory semantics as the capture of the monitor.
Accumulation of knowledge points
    • 1, String str1 = "abc", "ABC" is allocated in the memory character constant area;

    • 2, the program counter is a relatively small area of memory, used to indicate that the current thread executes the bytecode executed to the first line, is thread-isolated;

    • 3. Virtual machine stack describes the memory model of Java method execution, which is used to store local variables, operand stacks, dynamic links, method exits and other information, and is thread-isolated;

    • 4. In principle, all objects allocate memory on the heap, which is shared between threads.

Java Memory and data area

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.