Reprinted-10 key points of Java heap memory

Source: Internet
Author: User
Tags xms jconsole

When I started learning Java programming, I didn't know what heap memory or heap space is, and I didn't even know where the objects were put when they were created. When I started writingProgramLater, I often encounter errors in Java. Lang. outofmemoryerror, And then I began to focus on what is heap memory or heap space ). Most programmers have experienced this process, because it is very easy to learn a language, but the learning foundation is very difficult, because there is no specific process that allows you to learn every foundation of programming and discover the secrets of programming.

For programmers, it is very important to know the heap space, set the heap space, and handle the outofmemoryerror of the heap space. This Java heap tutorial is for my first-time programmers. If you know this basic knowledge or what is happening at the underlying layer, of course it may not be that helpful. Unless you know that the object is created in the heap, you will not realize that the outofmemoryerror occurs in the heap space. I try my best to write down all the knowledge I know about heap. I also hope that you can contribute as much as possible and share your knowledge so that other people can benefit from it.

What is the heap space in Java?

When the Java program starts to run, the JVM obtains some memory from the operating system. The JVM uses these memories, part of which is the heap memory. The heap memory is usually arranged at the bottom layer of the storage address and arranged up. After an object is created using the new keyword or other methods, the object obtains the memory from the heap. When the object is no longer used and is recycled as garbage, the memory will return to the heap memory again. 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's heap space is 128 MB, but there are exceptions, for example, in 32 non-Solaris operating systems (for the iSCSI platform Version) the default maximum heap space and start heap space are-XMS = 3670 K and-xmx = 64 m. For 64-bit operating systems, the size of heap space increases by about 30%. However, if you use the Java 1.5 throughput garbage collector, the default maximum heap size is 1/4 of the physical memory, and the initial heap size is 1/16 of the physical memory. To know the default heap size method, you can use the default setting parameter to open a program and use jconsole (supported after JDK 1.5, the maximum heap size is displayed on the VM summary page.

In this way, you can change the heap memory size based on your program needs. I strongly recommend this method instead of the default value. If your program is large and many objects need to be created, you can use the-XMS and-xmx parameters to change the heap memory size. XMS indicates the starting heap memory size, and xmx indicates the maximum heap memory size. In addition, there is a parameter-xmn, which indicates the size of New Generation (which will be mentioned later. One thing you need to note is that you cannot change the heap memory size at will. You can only set it when starting JVM.

Heap and garbage collection

We know that an object is created in the heap memory and garbage collection process clears the dead object from the heap space and returns the memory to the heap. To be used by the garbage collector, the heap is divided into three areas: New Generation, old generation, tenured generation, and perm space. New generation is used to store the space of the new object and is used when the object is created. If they are still in use for a long time, they will be moved to the old generation (or tenured generation) by the garbage collector ). Perm space is the place where JVM stores meta data, such as classes, methods, string pools, and class-level details. You can view "How garbage collection works in Java" for more information about heap and garbage collection.

Outofmemoryerror in Java heap

When the JVM is started, the-XMS parameter is used to set the memory. When the program continues to create more objects, JVM starts to expand the heap memory to accommodate more objects. JVM also uses the Garbage Collector to recycle memory. When the maximum heap memory set by-xmx is reached, the JVM will throw Java if no more memory can be allocated to new objects. lang. outofmemoryerror: Your program will be replaced. Before throwing an outofmemoryerror, JVM will try to use the Garbage Collector to free up enough space, but this error will be thrown when it finds that there is still not enough space. To solve this problem, you need to know the information of your program objects, such as the objects you have created and the space occupied by these objects. You can use profiler or heap analyzer to handle outofmemoryerror ." Java. Lang. outofmemoryerror: Java heap space "indicates that the heap does not have enough space and cannot be expanded ." Java. Lang. outofmemoryerror: permgen space "indicates that permanent generation is full and your program cannot be mounted to a class or be assigned 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 processing memory leaks and Java. Lang. outofmemoryerror. There are some tools in JDK To Help You Get heap dump, and some heap analysis tools to help you analyze heap dump. You can use "jmap" to obtain heap dump, which helps you create heap dump files. Then, you can use "jhat" (heap analysis tool) to analyze these heap dump files.

Ten Key Points of Java heap memory:

1. the Java heap memory is part of the memory allocated to the JVM by the operating system.

2. When we create objects, they are stored in the Java heap memory.

3. To facilitate garbage collection, Java heap space is divided into three areas: New Generation, old generation, tenured generation, and perm space.

4. You can use the JVM command line options-XMS,-xmx, and-xmn to adjust the size of the Java heap space. Do not forget to add "M" or "G" behind the size to indicate the unit. For example, you can use-xmx256m to set the heap memory size to 256 MB.

5. You can use jconsole or runtime. maxmemory (), runtime. totalmemory (), runtime. freememory () to view the heap memory size in Java.

6. You can use the command "jmap" to obtain heap dump and use "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 recycle the memory occupied by dead objects (no longer used objects) and then release them to the Java heap space.

9. when you encounter Java. lang. when outofmemoryerror occurs, Do not be nervous. Sometimes you only need to increase the heap space. If this happens frequently, check whether there is memory leakage in the Java program.

10. Use profiler and heap dump analysis tools to view the Java heap space. You can view the memory allocated to each object.

ArticleFrom: Java Revisited

Translated by: bole online

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.