JVM Heap Memory Management related

Source: Internet
Author: User
http://blog.csdn.net/kobejayandy/article/details/8496651
Four, Heap heap
The Heap (heap) is the memory data area of the JVM. Heap's management is complex, allocating an indefinite amount of memory each time, specifically to hold an instance of the object. Allocating a certain amount of memory in the heap to save the object instance is actually just saving the object instance's property value, the attribute's type and the object's own type tag, and so on, does not save the object's method (the method is the instruction, saves in the stack), in heap A certain amount of memory is allocated in the Save object instance and the object's serialization comparison is similar. When an object instance is allocated in heap, a 4-byte heap memory address needs to be stored in the stack to locate the object instance in the heap, so that it is easy to find the object instance.
Java heap is made up of all thread-sharingA piece of memory area.
4.1 Generation
The JVM heap can generally be divided into the following three parts:
Perm
Perm mainly save Class,method,filed objects, this section of the space generally will not overflow, unless a lot of loading the class, but when it comes to hot deployment of the application server, sometimes encounter Java.lang.OutOfMemoryError: PermGen space error, the cause of this error is likely to be redeployed every time, but after redeployment, class classes have not been unloaded, causing a large number of class objects saved in the perm, in this case, A general restart of the application server can resolve the problem.
tenured
Tenured the main preservation life cycle of the object, generally some old objects, when some objects in young replication transfer a certain number of times, the object will be transferred to the tenured area, generally if the system with the application level of caching, Objects in the cache are often moved to this interval.
Young
The young district is divided into three parts, the Eden area and the two survivor areas of the same size, of which only one of them is used in the Survivor interval, and the other is a copy object for garbage collection when the young is full, minor The GC will move the surviving objects into the free survivor interval, according to the JVM's strategy, after several garbage collections, the objects that are still alive in survivor will be moved to the tenured interval. 4.2 Sizing The Generations
The JVM provides the appropriate parameters to configure the memory size. As described above, the heap in the JVM is divided into 3 large intervals, while the JVM also provides some options for controlling the size of the young,tenured.
Total Heap
-XMS: Specifies the initialization of memory after the JVM's initial startup
-XMX: Specifies the maximum memory for the JVM heap, and allocates the memory of the specified size of the-XMX parameter to the JVM after the JVM is started, but not necessarily all, and the JVM adjusts the memory that is actually used for the JVM based on the-XMS parameters
The difference between the-XMX-XMS is the size of three virtual spaces
Young Generation
-xx:newratio=8 means the ratio of tenured and young is 8:1, so EDEN+2*SURVIVOR=1/9
Heap Memory
-xx:survivorratio=32 means that the ratio of Eden to a survivor is 32:1, a survivor that accounts for 1/34 of young.
The-XMN parameter sets the size of the young generation
Perm Generation
-xx:permsize=16m-xx:maxpermsize=64m
Thread Stack
-xx:xss=128k
load class. The 4th step is to load the class. Because the loader is not specified, the JVM defaults to using the bootstrap loader, loading all classes under Rt.jar into the persistent storage area in the heap class, and the custom class is loaded into memory
Specifies the value passed by the native data type, the address of the reference type
Request free memory to the operating system. The JVM says "Give me 64M of free memory" to the operating system, so the 1th step, the JVM to the operating system to request free memory for the system to find their own memory allocation table, find a segment of 64M memory to write the "Java Occupy" tab, and then the memory of the starting address and termination address to JVM,JVM ready to load class files
Q: Why does it produce outofmemory? A: There is not enough memory available in the HEAP memory. This sentence to understand, not to say Heap no memory, is that the new application of memory object is larger than Heap free memory, such as now HEAP also idle 1M, but the new application of the memory needs 1.1M, so will be reported OutOfMemory, may later object application of memory as long as 0.9M , so it just happens once OUTOFMEMORY,GC is normal, it looks like an accident, that's what happened. However, if the GC does not recycle at this time, a suspend condition is generated and the system does not respond.
Q: I don't have many objects and why I have outofmemory. I don't have a lot of objects.             A: Your inheritance hierarchy is much, the object generated in the Heap is the first generation of the parent class, and then the subclass, understand not. Errors are divided into several. Q: OutOfMemory errors are divided into several. A: There are two kinds, respectively, "Outofmemoryerror:java Heap Size" and "Outofmemoryerror:permgen space", both are memory overflow, heap size is said to apply for new memory, this is very common, Check to apply or adjust heap memory size. "PermGen space" is because the permanent storage area is full, this is also very common, generally in the hot release of the environment, because each release application system is not restarted, over time the permanent storage area of dead objects too many cause new objects can not apply for memory, generally restart it.
Q: Why does it produce stackoverflowerror? A: Because a thread runs out of Stack memory, it is usually caused by a recursive function.
Q: Can you see multiple JVMs on a machine? Can the JVM exchange visits between the two? A: You can have multiple JVMs, as long as the machine is able to withstand. There is no exchange of visits between JVMs, and you cannot access B-JVM Heap memory in A-JVM, which is impossible.
Q: In the JVM, which areas are shared. which are private. A: Heap and method area are shared, others are private,
Q: What is JIT, you did not say. , why didn't you say. A: JIT refers to Just in time, some documents to the JIT as a part of the JVM to introduce, or as part of the implementation of the engine to introduce, which can be understood. When Java was first born, it was an interpretive language, don't boo, even if it was compiled into bytecode (byte code) for the JVM, it needs to be translated again into native code (native) to be executed by the machine, and the worry of efficiency is raised. Sun has put forward a new set of mechanisms to solve this problem, OK, you want to compile into native code, no problem, I provide a tool on the JVM, the bytecode compiled into the original code, the next time you visit the direct access to the native code becomes, so JIT was born, and so on.
Q: What other parts of the JVM are not mentioned? A: The JVM is an incredibly complex thing to write a brick book, and there are a few things to explain: Chang (constant pool) in order to store the constants in the program: and the area where the index is numbered. For example int i = 100, this 100 is placed in a constant pool. Security Manager: Provides Java run-time security controls to prevent malicious attacks, such as specifying read files, write file permissions, network access, create processes, and so on, Class Loader to load C after the secure Manager certification passes Lass the file. Method Index Table (Methods table), which records the address information for each approach, and the address pointer in Stack and HEAP actually points to the Methods table address.
Q: Why not suggest an explicit life System.GC () in the program. A: Because the explicit declaration is to do heap memory scan, that is, full GC, is the need to stop all activities (Stop the World Collection), your application can withstand this.
Q: What tuning parameters are available to the JVM? A: Very much, to find their own, heap memory, stack memory size can be defined, or even the heap memory of the three parts, the new generation of the various proportions can be adjusted.

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.