Summary analysis based on 10 key points of Java heap memory

  • 2020-04-01 01:58:18
  • OfStack

10 keys to Java heap memory.
Javaoutofmemoryerrorgenerationjvmprofiler programming when I began to learn Java programming, I don't know what is a heap memory or heap space, I don't even know when object creation, where you left them. When I start to write some formal program, I will often meet with Java. Lang. OutOfMemoryError error, then I began to focus on what is a heap memory or heap space (heap space). Most programmers go through this process, because learning a language is very easy, but learning the basics is very difficult, because there is no specific process that allows you to learn each of the basics of programming and discover the secrets of programming.
It is important for programmers to know the heap space, set the heap space, handle the outOfMemoryError of the heap space, and analyze the heap dump. This tutorial on the Java heap is for my brother who is just starting programming. If you know the basics or what's going on underneath, it's probably not that helpful. Unless you know that objects are created in the heap, you will not realize that an OutOfMemoryError occurs in the heap space. I've written down everything I know about the heap, and I hope you can contribute and share your knowledge as much as possible so that others can benefit as well.
What is heap space in Java?
When a Java program starts running, the JVM takes some memory from the operating system. The JVM USES this memory, and part of that memory is heap memory. Heap memory is usually at the bottom of the storage address, arranged upward. When an object is created with the new keyword or otherwise, the object gets memory from the heap. When the object is no longer in use and is garbage collected, the memory is returned to the heap. To learn about garbage collection, read "how garbage collection works in Java."
How do I increase the Java heap space
In most 32-bit machine, the Sun on the JVM, the Java heap size to 128 MB by default, but there are exceptions, for example, in 32 not the Solaris operating system (SPARC platform version), the default maximum heap space and initial heap size for - and -xmx Xms = 3670 k = 64 m. for 64 - bit operating system, general heap size increased about 30%. But you use Java 1.5 throughput garbage collector, the default maximum heap size for a quarter of the physical memory, The starting heap is one-sixteenth the size of physical memory. To find the default heap size method, open a program with the default Settings parameter, view it using JConsole (supported after JDK 1.5), and see the maximum heap size on the VM Summary page.
In this way you can change the size of the heap according to the needs of your program, and I strongly recommend this method instead of the default. If your program is large and has a lot of objects to create, you can use -xms and -xmx to change the size of the heap. Xms represents the starting heap memory size, and Xmx represents the maximum heap memory size. Another parameter, -xmn, represents the size of the new generation (mentioned later). One thing to note is that you can't change the size of the heap memory arbitrarily, you can only set it when you start the JVM.
Heap and garbage collection
We know that objects are created in heap memory, and garbage collection is a process that clears dead objects out of heap space and returns that memory to the heap. For use by the garbage collector, the heap is mainly divided into three areas called New Generation, Old Generation or Tenured Generation, and Perm space. If used for a long time, they can be moved by the garbage collector to Old Generation (or Tenured Generation). Perm space is where the JVM stores Meta data, such as class, method, string pool, and class level details. You can see how garbage collection works in Java for more information on heap and garbage collection.
OutOfMemoryError in the Java heap
When the JVM starts, the pair of memory set by the -xms parameter is used. As the program continues to create more objects, the JVM begins to expand the heap memory to accommodate more objects. The JVM also USES a garbage collector to reclaim memory. When fast achieve the -xmx setting the maximum heap memory, if there is no more memory can be assigned to new objects, the JVM will be thrown. Java lang. Outofmemoryerror, your program will fail. Before throwing an OutOfMemoryError, the JVM tries to free up enough space with the garbage collector, but when it finds that there is still not enough space, it throws the error. To solve this problem, you need to be clear about your application objects, such as which objects you create, how much space those objects take up, and so on. You can use profiler or heap analyzer to handle outofmemoryerrors. "Java. Lang. OutOfMemoryError: Java heap space," said there was no enough space in the heap, cannot continue to expand. "Java. Lang. OutOfMemoryError: PermGen space," said permanent generation has been filled, your application cannot be placed in a class or assign a string.
The Java Heap dump
Heap dump is a snapshot of the Java Heap memory at a certain time. It for analyzing heap memory or memory leaks and Java. Lang. An outofmemoryerror is very useful. There are tools in the JDK to get heap dumps for you, and there are also heap analysis tools to analyze heap dumps for you. You can get heap dumps with "jmap", which creates heap dump files for you, and then you can analyze them with "jhat".
Ten keys to Java heap memory:
1. The Java heap memory is a portion of the memory allocated by the operating system to the JVM.
2. When we create objects, they are stored in Java heap memory.
3. To facilitate garbage collection, the Java heap Space is divided into three sections, called New Generation, Old Generation or Tenured Generation, and Perm Space.
4. You can adjust the size of the Java heap space by using the JVM's command-line options -xms, -xmx, -xmn. Don't forget to add "M" or "G" after the size to denote the units. For example, you can set the maximum heap size to 256MB with -xmx256m.
5. You can use JConsole or runtime.maxmemory (), runtime.totalmemory (), runtime.freememory () to see the size of the heap memory in Java.
6. You can use the command "jmap" to obtain heap dump and "jhat" to analyze heap dump.
7. The Java heap space is different from the stack space, which is used to store the call stack and local variables.
8. The Java garbage collector is used to reclaim memory from dead objects (objects that are no longer in use) and release it into Java heap space.
9, when you meet a Java. Lang. OutOfMemoryError, don't be nervous, sometimes just increasing heap space is ok, but if often appear, to see whether there are memory leaks in Java program.
Use profilers and Heap dump analysis tools to view Java Heap space to see how much memory is allocated to each object.

Related articles: