Summary and analysis of 10 key points based on Java heap memory _java

Source: Internet
Author: User
Tags garbage collection xms jconsole
10 main points of Java heap memory.
Javaoutofmemoryerrorgenerationjvmprofiler programming When I started learning Java programming, I didn't know what heap memory or heap space was, and I didn't even know where the objects were when they were created. When I started to write some programs, I often encountered java.lang.outOfMemoryError error, then I began to focus on what is heap memory or heap space (heap spaces). Most programmers have experienced this process, because learning a language is very easy to come by, but learning the basics is very difficult, because there is no specific process that allows you to learn every basis of programming, so that you find the secret of programming.
It is very important for programmers to know heap space, set heap space, deal with outofmemoryerror errors in heap space, and analyze heap dump. This tutorial on the Java heap is for the brothers I just started to learn programming. If you know the basics or know what's going on at the bottom, of course it might not help that much. Unless you know that an object is created in a heap, you will not realize that outofmemoryerror occurs in heap space. I have written down all the knowledge I know about the heap as much as I can, and I hope that you will be able to contribute and share your knowledge to the benefit of other people as well.
What is the heap space in Java?
When the Java program starts running, the JVM acquires some memory from the operating system. The JVM uses these memory, and part of that memory is heap memory. Heap memory is usually at the bottom of the storage address, arranged up. When an object is created through the new keyword or otherwise, the object obtains memory from the heap. When the object is no longer in use and is reclaimed as garbage, the memory is back in 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, Sun's JVM, Java's heap space defaults to 128MB, but there are exceptions, for example, on the 32 Solaris operating system (SPARC platform version), the default maximum heap space and start heap space size are-xms=3670k and-xmx= 64M. For 64-bit operating systems, the general heap space size is increased by about 30%. But you use Java 1.5 's throughput garbage collector, the default maximum heap size is One-fourth of physical memory, and the starting heap size is one-sixteenth of physical memory. To know the default heap size method, you can open a program with the default setting parameters, using Jconsole (which is supported after JDK 1.5) to see the maximum heap size on the VM summary page.
In this way you can change the heap memory size according to the needs of your program, and I strongly recommend this approach rather than the default value. If your program is large and many objects need to be created, you can use-XMS and-xmx these two parameters to change the size of the heap memory. XMS represents the initial heap memory size, XMX represents the maximum heap memory size. There is also a parameter-xmn, which represents the size of the new generation (which will be mentioned later). One thing you should be aware of is that you can't arbitrarily change the size of the heap memory, you can only set it up 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 the dead objects out of heap space and returns them back to the heap. In order to use the garbage collector, the heap is mainly divided into three regions, called the new Generation,old Generation or tenured Generation, and perm space. The new generation is used to store the newly created object in space and is used when the object is new. If they are used for a long time, they will be moved by the garbage collector to the old Generation (or tenured Generation). Perm space is where the JVM holds meta data, such as classes, methods, string pools, and class-level details. You can look at how garbage collection works in Java to get more information about heap and garbage collection.
OutOfMemoryError errors in the Java heap
When the JVM is started, the memory is set using the-XMS parameter. As the program continues to create more objects, the JVM begins to expand heap memory to accommodate more objects. The JVM also uses the garbage collector to reclaim memory. As soon as the maximum heap memory set by-XMX is reached, if no more memory can be allocated to the new object, the JVM throws java.lang.OutOfMemoryError, and your program is dropped. Before throwing the OutOfMemoryError, the JVM tries to use the garbage collector to free up enough space, but when it finds that there is still not enough space, it throws the error. To solve this problem, you need to know the information about your program object, for example, what objects you create, what objects occupy a lot of space, and so on. You can use Profiler or heap analyzer to handle outofmemoryerror errors. "Java.lang.OutOfMemoryError:Java heap space" means that there is not enough room for the heap to continue expanding. "Java.lang.OutOfMemoryError:PermGen space" means permanent generation is full, your program can no longer be installed in the class or assign a string.
Java Heap Dump
Heap dump is a snapshot of the Java heap memory at a certain time. It is useful for analyzing heap memory or handling memory leaks and Java.lang.outofmemoryerror errors. There are some tools in the JDK to help you get heap dump, and there are some heap analysis tools to help you analyze heap dump. You can use "Jmap" to get heap dump, which helps you create heap dump files, and then you can use "Jhat" (Heap analysis tool) to analyze these heap dump.
10 main points of Java heap memory (heap memory):
1. Java heap Memory is part of the memory that the operating system allocates to the JVM.
2, when we create objects, they are stored in the Java heap memory.
3, in order to facilitate garbage collection, Java heap space into three areas, respectively called New Generation, old Generation or called tenured Generation, there are perm spaces.
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" to the size to indicate the unit. For example, you can use-xmx256m to set the maximum size of the heap memory to 256MB.
5, you can use Jconsole or runtime.maxmemory (), Runtime.totalmemory (), runtime.freememory () to see the size of heap memory in Java.
6, you can use the command "Jmap" to obtain heap dump, with "jhat" to analyze heap dump.
7, Java heap space is different from stack space, stack space is used to store call stacks and local variables.
8. The Java garbage collector is used to reclaim memory from dead objects (objects that are no longer used) and then to release them into the Java heap space.
9, when you encounter java.lang.outOfMemoryError, don't be nervous, sometimes just add heap space on it, but if often, it is necessary to see if there is memory leak in the Java program.
10. Use the profiler and heap dump analysis tools to view the Java heap space and to see how much memory is allocated to each object.
Related Article

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.