This article is a detailed analysis and introduction to the 10 key points of Java heap memory. Friends in need can refer to it
10 Points of Java Heap Memory.
javaoutofmemoryerrorgenerationjvmprofiler Programming When I started learning Java programming, I didn’t know what heap memory or heap space was, I even Don't know where the objects are placed when they are created. When I started to write some formal programs, I would often encounter java.lang.outOfMemoryError errors, and then I started to pay attention to what heap memory or heap space is. Most programmers have experienced this process, because it is very easy to learn a language, but it is very difficult to learn the basics, because there is no specific process for you to learn every basic of programming and make you discover programming. The secret.
It is very important for programmers to know the heap space, set up the heap space, handle outOfMemoryError errors in the heap space, and analyze heap dump. This tutorial on Java heap is for my brother who just started to learn programming. Of course it might not be that helpful if you know the basics or know what's going on under the hood. Unless you know that the object is created in the heap, you will not realize that the OutOfMemoryError occurs in the heap space. I have tried my best to write down everything I know about heaps, and I hope you can contribute and share your knowledge as much as possible so that others can benefit from it.
What is the heap space in Java?
When a Java program starts running, the JVM will obtain some memory from the operating system. The JVM uses this memory, and part of this memory is heap memory. Heap memory is usually at the bottom of the storage address, arranged upward. When an object is created using the new keyword or by other means, the object obtains memory from the heap. When the object is no longer used and is collected as garbage, the memory is returned to the heap memory. To learn about garbage collection, read "How Garbage Collection Works in Java".
How to increase Java heap space
On most 32-bit machines and Sun's JVM, the default size of Java heap space is 128MB, but there are exceptions, such as On 32-bit Solaris operating system (SPARC platform version), the default maximum heap space and starting heap space size are -Xms=3670K and -Xmx=64M. For 64-bit operating systems, the general heap space size increases by about 30%. But if you use the throughput garbage collector of Java 1.5, the default maximum heap size is one-quarter of the physical memory, and the starting heap size is one-sixteenth of the physical memory. To know the default heap size, you can open a program with the default setting parameters and use JConsole (supported after JDK 1.5) to view it. You can see the maximum heap size on the VM Summary page.
With this method you can change the heap memory size according to the needs of your program, I strongly recommend using this method instead of the default value. If your program is large and there are many objects that need to be created, you can use the two parameters -Xms and -Xmx to change the size of the heap memory. Xms represents the starting heap memory size, and Xmx represents the maximum heap memory size. There is also a parameter -Xmn, which indicates the size of the new generation (will be mentioned later). One thing you need to note is that you cannot change the heap memory size arbitrarily, you can only set it when starting the JVM.
Heap and Garbage Collection
We know that objects are created in heap memory. Garbage collection is a process that clears dead objects from the heap space and removes them. The memory is then returned to the heap. In order to be used by the garbage collector, the heap is mainly divided into three areas, called New Generation, Old Generation or Tenured Generation, and Perm space. New Generation is a space used to store newly created objects and is used when objects are created. . If they are still used for a long time, they will be moved to Old Generation (or Tenured Generation) by the garbage collector. Perm space is where the JVM stores Meta data such as classes, methods, string pools and class-level details. You can check out "How Garbage Collection Works in Java" for more information about the heap and garbage collection.
OutOfMemoryError in Java Heap
When the JVM starts, the memory set by the -Xms parameter is used. As the program continues and more objects are created, the JVM begins to expand the heap memory to accommodate more objects. The JVM also uses the garbage collector to reclaim memory. When the maximum heap memory set by -Xmx is almost reached, if no more memory can be allocated to new objects, the JVM will throw a java.lang.outofmemoryerror and your program will crash. Before throwing OutOfMemoryError, the JVM will try to use the garbage collector to free up enough space, but if it finds that there is still not enough space, it will throw this error. In order to solve this problem, you need to know the information about your program objects, for example, which objects you created, how much space each object takes up, etc. You can use a profiler or heap analyzer to handle OutOfMemoryError errors. "java.lang.OutOfMemoryError: Java heap space" means that the heap does not have enough space and cannot continue to expand. "java.lang.OutOfMemoryError: PermGen space" means that the permanent generation is full and your program can no longer be loaded into classes or allocate a string.
Java Heap dump
Heap dump is a snapshot of Java heap memory at a certain time. It is very useful for analyzing heap memory or dealing with memory leaks and Java.lang.outofmemoryerror errors. There are some tools in the JDK that can help you get heap dumps, and there are also some heap analysis tools that can help you analyze heap dumps. You can use "jmap" to get heap dumps, which helps you create heap dump files. Then, you can use " jhat" (heap analysis tool) to analyze these heap dumps.
Ten key points of Java heap memory (heap memory):
1. Java heap memory is an operation The portion of memory allocated by the system to the JVM.
2. When we create objects, they are stored in Java heap memory.
3. In order to facilitate garbage collection, the Java heap space is divided into three areas, called New Generation, Old Generation or Tenured Generation, and Perm Space.
4. You can use the JVM command line Options -Xms, -Xmx, -Xmn to adjust the size of Java heap space. Don’t forget to add “M” or “G” after the size to indicate the unit. For example, you can use -Xmx256m to set the maximum size of heap memory to 256MB.
5. You can use JConsole or Runtime.maxMemory(), Runtime.totalMemory(), Runtime.freeMemory() to view Java The size of the heap memory.
6. You can use the command "jmap" to obtain heap dump, and use "jhat" to analyze the heap dump.
7. Java heap space is different from stack space. Stack space is used to store call stacks and local variables. of.
8. The Java garbage collector is used to reclaim the memory occupied by dead objects (objects that are no longer used) and then release it into the Java heap space.
9. Don’t be nervous when you encounter java.lang.outOfMemoryError. Sometimes just increasing the heap space is enough, but if it occurs often, you need to see if there is a memory leak in the Java program.
10. Please use Profiler and Heap dump analysis tools to view the Java heap space. You can see how much memory is allocated to each object.
The above is the detailed content of Share ten key points of Java heap memory. For more information, please follow other related articles on the PHP Chinese website!