Some explanations on memory management of Java Virtual machine

Source: Internet
Author: User
Tags xms

Java Data type:

1) Original type: Primitive Types (original value)

Numeric type (Numeric Types)    integral type (Integral Types), floating-point type (floating-point Types) Boolean type (Boolean Types) returnaddress type: An opcode (Opcode) that represents a bytecode instruction. Of all the primitive types supported by the virtual machine, only the returnaddress type cannot correspond directly to the data type of the Java language.

2) Reference type: Reference Types (reference value)

Class type (class Types) array type (array Types) interface type (Interface Types)

Run-time data area:

PC (program Counter) Register: In a multithreaded environment, each thread has its own PC register, which saves the Java Virtual machine executing bytecode instruction address of the Java Virtual machine stack (stack): created concurrently with the thread to store local variables.    Base data type, save reference.        The advantage of the stack is that the access speed is faster than the heap, second only to the registers directly in the CPU. 1) If a thread requests an allocated stack capacity that exceeds the maximum allowable capacity of the virtual machine, the Stackoverflowerror exception is thrown (? How to view the maximum capacity of a virtual machine stack) 2) If the Java Virtual machine stack can be dynamically extended, but the attempt cannot request enough memory to create the corresponding virtual machine stack, it throws the OutOfMemoryError exception Java heap (heap): Created at virtual machine startup, available for each thread    A shared run-time memory area, which is also an area for all class instances (values) and array object (value) memory allocations. Whenever you create a new object with new (), it is created in the heap that stores the various objects managed by the garbage collector (garbage Collector), which do not have to be destroyed explicitly. Virtual machine implementation can choose the memory management technology (garbage collection Technology) heap capacity can be dynamically expanded, the space automatically shrink, (?) according to the actual demand. How to initialize the size of the heap) 1) if the actual required heap exceeds the maximum capacity that memory management can provide, then the virtual machine throws the OutOfMemoryError exception method area: When the virtual machine starts, it creates a region of the runtime memory that can be shared by each thread, saving The structure information for each class is stored 1) if the memory space of the method area does not meet the memory allocation request, the Java virtual machine throws a OutOfMemoryError exception. Runtime Constant Pool: Each run-time pool is assigned a runtime representation of the constant pool of each class and interface in the method area of the Java virtual machine, and the numeric literal is known at compile time.        Once the classes and interfaces are loaded into the virtual machine, the corresponding run-time pools are created. 1) When you create a class or interface, the Java virtual machine throws a OutOfMemoryError exception if the amount of memory required to construct the run-time pool exceeds the maximum value that can be provided by the method area. Local method Stack: Use to traditional stacks (commonly referred to as "C Stacks") to support the execution of the native method, which refers to methods written in languages other than Java, this stackis the local method stack the Java virtual machine throws a Stackoverflowerror exception if the thread requests an allocated stack capacity that exceeds the maximum allowable capacity of the local method stack. If the local method stack can be dynamically extended, and the extended action has been tried, but the current cannot request enough memory to complete the extension, or if there is not enough memory to create the corresponding local method stack when the new thread is created, the Java virtual machine will throw a OutOfMemoryError exception.

Add:

1) heap and non-heap (non-heap) memory according to the official statement: "Java virtual machines have a heap, the heap is a runtime data region, and all class instances and arrays of memory are allocated from here." The heap is created when the Java virtual machine is started. "" The memory outside the heap in the JVM is called non-heap (non-heap memory) ". You can see that the JVM primarily manages two types of memory: heap and non-heap. Simply put, the heap is the Java code of memory, is left to the "developer", not the heap is the JVM left to itself, so the method area, the JVM internal processing or optimization of the required memory (such as the JIT compiled code cache), each class structure (such as running a constant pool, field and method data) And the code for methods and construction methods are in non-heap memory. 2) heap memory allocation JVM initially allocates memory specified by-XMS, the default is physical memory 1/64;JVM the maximum allocated memory is specified by-XMX, which defaults to 1/4 of physical memory. When the default free heap memory is less than 40%, the JVM increases the heap until the maximum limit of-xmx, and when the free heap memory is greater than 70%, the JVM reduces the heap until the minimum limit of-XMS. So the server generally sets-xms,-xmx equal to avoid resizing the heap after each GC. 3) Non-heap memory allocation The JVM uses-xx:permsize to set the non-heap memory initial value, which defaults to 1/64 of the physical memory, and the maximum non-heap memory by Xx:maxpermsize, which defaults to 1/4 of the physical memory. 4) JVM Memory limit (max) First JVM memory is limited to the actual maximum physical memory (nonsense!). hehe), assuming that the physical memory is infinitely large, the maximum value of the JVM memory is very much related to the operating system. In short, the 32-bit processor, although the controllable memory space has 4GB, but the specific operating system will give a limit, This limit is generally 2GB-3GB (typically under Windows systems for the 1.5g-2g,linux system 2g-3g), and processors over 64bit are not limited.

Instance:
Student Student = new Student ();
This line of code created two memory space, the first in the stack, called student, which is equivalent to the pointer type of variable, it does not hold the student's name, age and other specific values, these values are stored in the heap

About STRINGSTR = "abc" in-house work. Inside Java, this statement is translated into the following steps:
(1) First define an object reference variable named str to the String class: String str;
(2) in the "stack" to find if there is a value of "ABC" address, if not, then open a store with a literal "ABC" address, then create a new String Class object O, and the string value of O point to the address, and in the stack next to this address note that the referenced object o. If you already have an address with a value of "ABC", look for the object o and return the address of O.

==========================================================================

Import java.util.*;p ublic class test{public    static void main (String []args) {        int size=1;        Student[] Students=new student[size];        for (int i=0;i<size;i++) {            students[i]=new Student ();        }        Runtime runtime=runtime.getruntime ();        System.out.println (Runtime.totalmemory ());//Gets the amount of memory that the JVM allocates to the program size=1:16252928    size=1000000:494317568        System.out.println (Runtime.freememory ());//Gets the amount of memory currently available              15965128                 32584224        System.out.println ( Runtime.maxmemory ());//Gets the maximum amount of memory that the JVM can request        259522560                518979584    }}class student{    Private List <String> list=new arraylist<string> (100);}

Java.lang.OutOfMemoryError:Java Heap Space exception thrown when running directly
If you use

Java-xms256m-xmx512m Test, it runs normally

Description:
-xms:set initial Java heap size (set JVM initialization heap memory size)
Default is physical memory 1/64
-xmx:set maximum Java heap size (set JVM max heap Memory size)
The default is physical memory of the quarter
when the free heap memory is less than 40%, the JVM increases the heap until the maximum limit of-xmx, and when the free heap memory is greater than 70%, the JVM reduces the heap until the minimum limit of-XMS.
Therefore the server is generally set-XMS,-xmx equal to avoid resizing the heap after each GC.

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.