Java Virtual Machine memory management

Source: Internet
Author: User
Tags xms

  • Data area for Virtual runtime
    • Thread sharing
      • Java Heap
        • Java Heamp is the largest memory area in which Java virtual machines can be configured, and the only purpose is to store instance objects, shared by all threads;
        • GC Heap, the recycling technology mainly uses the generational collection algorithm; Java heap is subdivided into: Cenozoic, Laosheng generation; The partitioning itself is not related to the contents of storage; No matter how divided, it is to better allocate and reclaim memory;
        • Logically continuous, not physically contiguous;
        • The Outofmerroyerror error will be thrown when the configuration cannot be allocated and extended by-XMS and-xmx;
      • Method area
        • Storage objects: Class information loaded by virtual machines, constants, static variables, JIT compiler-compiled code
        • Alias Name:non-heap
        • Hotspot virtual machine, Permanent Generation (permanent generation), in fact, the two are not equivalent, the GC is extended to the method area;
    • Thread Isolation
      • Native Method Stack
      • Program Counter Register
        • Thread-Private;
        • Java Virtual Machine multithreading switches and allocates processor execution time by thread;
        • Multi-threaded rotation switch, it is necessary to restore the thread in a moment at the last execution of the address, for example, a thread executing a method, the counter record is the thread execution of the virtual machine bytecode instruction address, if it is the native method, then the value of the counter is empty (undefined)
        • The only memory without OutOfMemoryError
      • VMS Stack
    • Direct Memory
      • Direct memory is not part of the data area when the virtual runtime is running;
      • NIO (new Input/output), channel+buffer new IO, uses the native library to directly allocate out-of-heap memory, using a Directbytebuffer object stored in java-heap to directly reference the block memory The advantage is to avoid copying data back and forth between Java-heap and Native-heap.
      • Outofmerroyerror error;
  • Object
    • Memory allocation method
      • Pointer collisions (bump the pointer)---parnew, Serial
      • Free list---CMS (mark-sweep)
    • Decision conditions
      • Whether the garbage collector is ==>java-heap with compressed finishing
    • Memory allocation policy
      • Purpose: Resolve Conflict issues
      • Method One: CAS + Error retry ===> to ensure atomicity of the update operation
      • Method Two: The thread allocates memory (JAVA-HEAP) in different spaces, pre-allocating memory is called Tlab (thread Local Allocation Buffer), the thread needs to allocate memory, it is allocated on the tlab of that thread, Only when the Tlab is used to allocate the new Tlab, the synchronization lock is required, whether the virtual machine uses Tlab and the-xx:+/-usetlab parameter configuration;
    • Memory layout
      • Header, Instance Data, Padding (align padding)
      • Header: object's hashcode\ biased thread id\gc generational age \ Lock state flag \ Type pointer (pointer to its class metadata)
      • padding no real meaning; guaranteed data length is an integer multiple of 8 bytes
      • Instance Data: The parent class and subclass may cross-store, and smaller sub-class variables may be inserted into the void of the parent class variable, and fields of the same length are always assigned together, such as: Shorts/chars, Longs/doubles
    • Object access
      • Handle, Direct pointer
      • Difference
        • Handle Way Access Data---reference (handle address)--Handle pool (hold data pointer)---> object pointer--data
          • Advantage: Data movement, reference not moving
        • Direct pointer----Reference (object pointer address)--Object pointer---data
          • Advantage: access to data blocks
  • JVM Error
    • JAVA-HEAP Configuration
      • -xms (Heap min),-xmx (heap max)
        • Not extensible: Set the same
        • -xx:+heapdumponoutofmemoryerror: When a memory overflow exception occurs, dump the storage snapshot of the current memory heap
    • Classification
      • Heap Overflow
        • Memory leak (RAM leak)
        • Memory Overflow (overflow)
      • Stack overflow (virtual machine, local method)
        • -XSS decision
        • Scene appears
          • Thread request stack depth > virtual machine allow depth-->stackoverfolwerror
          • The virtual machine did not request enough memory on the scale stack-->outofmemoryerror
      • Method area, run-time-constant pool
        • -xx:permsize-xx:maxpermsize
        • String.intern (), native method, function: Returns if there is a corresponding string object in the constant pool, and if not, puts the corresponding string into the constant pool; use: string.valueof (i++). Intern ()
      • Direct Memory
        • -xx:maxdirectmemorysize, if not specified, the default equals-XMX value is the same

Java Virtual Machine memory management

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.