JVM Learning Series (a) JAVA area of memory and memory overflow exception

JAVA memory area introduction

uK3Y90.png

Program Counter:

  • Thread private, small memory space, it can be seen as the line number indicator byte code executed by the current thread;
  • Each thread has a separate program counter, between the individual threads not affect each other, independently of the storage;
  • If the thread is executing a Java method, this counter records the address of the virtual machine bytecode instruction is being executed, if it is a Native method, then the value of the counter was undefined;
  • (Not JAVA Virtual Machine Specification Specification) where the memory area of ​​any OutOfMemoryError does not occur.
  • goto reserved word (Java do not have the current version for the time being, do not let others use), jump to a specific row, the program counter is actually in operation.

    In the conceptual model of virtual machines, the bytecode interpreter of the work is to be removed by changing the value of the program counter bytecode instructions to be executed a branch, looping, branching, exception handling, and so will need to restore the thread rely on this program counter.

Java Virtual Machine stack

  • Thread private, life cycle and thread the same;
  • JAVA virtual machine execution method service;
  • The method described dynamic memory JAVA execution model;
  • Stack frame: Each method creates a stack frame at the time of execution, used to store information local variable table, the operand stack, dynamic links, and so on for export, from each method call to the process is complete, all of the virtual response machine to push the stack out of the stack process.
  • Local variable table:
    • All the basic data types stored compile knowable, reference types, returnAddress type
    • Local variable table memory space allocation is done at compile time, when entering a method, this method requires much memory allocation in the frame is fixed during the process of operation will not change the memory size of the local variable table.
  • If the stack is greater than the depth of the thread requested by the virtual machine allowable depth, StackOverflowError exception is thrown;
  • If the virtual machine can dynamically expand the stack (stack Jvm virtual machine can also be dynamic dynamic length), when extended to apply to not enough memory, OutOfMemoryError is thrown.

Native method stacks

  • Thread-private
  • Native method for the virtual machine to perform the service;
  • The role of the JAVA virtual machine stacks the same, but the scope of the difference;
  • If the requested native method stack depth greater than the depth allowed by the virtual machine, StackOverflowError exception is thrown;
  • If the virtual machine can dynamically expand the stack (stack Jvm virtual machine can also be dynamic dynamic length), when extended to apply to not enough memory, OutOfMemoryError is thrown.

JAVA heap

  • Threads share
  • The largest piece of memory JAVA virtual machine management;
  • Created when the virtual machine starts;
  • Storing object instance (all object instances and arrays);
  • The main area of ​​the garbage collector management;
  • Cenozoic years old, Eden space;
  • It may be in a discontinuous physical memory space, as long as can be logically contiguous;
  • Alternatively a fixed size or dynamically expanded;
  • -Xms -Xmx adjust the size and expansion of the initial size of the heap;
  • When no heap allocation and heap memory to complete the instance can not continue to expand, OutOfMemoryError is thrown.

Methods district

  • Threads share;
  • Class information storage has been loaded in the virtual machine, constants, static variables, the instant editor to compile the code and other data;
  • Types of information:
    • Version of the class
    • Field
    • method
    • interface
  • Alternatively a fixed size or dynamically expanded;
  • You may choose not to implement garbage collection;
  • When memory allocation can not be met, OutOfMemoryError is thrown.

Runtime constant region

  • Region part of the method;
  • Class file versions in addition to the classes, fields, methods and other descriptive information, as well as the constant pool, the various symbols literal and mainly for storage of generated reference compiled into the method area when the class is loaded runtime constant pool;
  • Because the area belong to the part of the method, when the memory allocation can not be met, OutOfMemoryError is thrown.

Direct Memory

  • Not part of the runtime data area, nor is the JAVA virtual machine specification memory region defined;
  • No JAVA memory limit, but is limited by the physical memory, the sum of the respective memory about the physical memory limitations, occurs when an OutOfMemoryError dynamic expansion.

    In JDK1.4 newly added NIO (new Input / Output) type, based on the introduction of a channel (Channel) and the buffer (Buffer) in the I / O mode, he can use Native libraries directly outside the heap memory allocated, then stored in the stack by a JAVA objects as references DirectByteBuffer this memory to operate, which can significantly improve performance by avoiding the replicate data in the heap and stack JAVA Native.

Point of doubt

  • String intern

Guess you like

Origin www.cnblogs.com/jakaBlog/p/11767881.html