10 Essentials of Java Heap memory

Source: Internet
Author: User
Tags xms jconsole

When I started to learn Java programming, I didn't know what was heap memory or heap space, and I didn't even know where they were when the object was created. When I began to formally write some programs, I would often encounter 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 the basics of programming and make you aware of the secrets of programming. For programmers, it is important to know heap space, to set up heap space, to handle outofmemoryerror errors in heap space, and to 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, it's probably not that big of a help. Unless you know that the object is created in the heap, you will not realize that OutOfMemoryError is occurring in the heap space. I have written 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 as much of your knowledge as possible so that others will benefit 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, which is part of the heap memory. Heap memory is usually at the bottom of the storage address and is arranged upward. When an object is created through the new keyword or by other means, the object obtains memory from the heap. When the object is no longer in use and is garbage collected, the memory is returned 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, Sun's JVMs, Java's heap space defaults to 128MB, but there are exceptions, such as the default maximum heap space and the initial heap space size of-xms=3670k and-xmx=64m on the 32 Solaris operating system (SPARC platform version). For 64-bit operating systems, the general heap size increases by about 30%. But you use the Java 1.5 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 setup parameters and use Jconsole (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 method instead of the default value. If your program is large and many objects need to be created, you can use-XMS and-xmx 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, that represents the size of the new generation (which is mentioned later). One thing you need to notice is that you can't arbitrarily change the size of the heap memory, you can only set it when you start the JVM.

Heap and garbage collection

We know that objects are created in heap memory, garbage collected such a process, it clears the dead objects out of the heap space, and the memory back to the heap. To use the garbage collector, the heap is divided into three areas, called New Generation,old Generation or tenured Generation, and perm space. New generation is the space used to store the newly created object, which is used when the object is newly created. If they are used for a long time, they will be moved to the 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 see how garbage collection works in Java to get more information about heaps and garbage collection.


OutOfMemoryError errors in the Java heap

When the JVM starts, the pair of memory is set using the-XMS parameter. When the program continues, creating more objects, 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 reached, if no more memory is allocated to the new object, the JVM will throw java.lang.OutOfMemoryError and your program will be 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 created, what objects took up a lot of space, and so on. You can use the profiler or the heap parser to handle outofmemoryerror errors. "Java.lang.OutOfMemoryError:Java heap space" means that the heap does not have enough room to continue to expand. "Java.lang.OutOfMemoryError:PermGen space" means that permanent generation is full, your program can no longer be loaded in class or 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 handling memory leaks and Java.lang.outofmemoryerror errors. There are some tools in the JDK that can help you get the heap dump and some heap analysis tools to help you analyze the heap dump. You can use "Jmap" to get heap dump, which helps you create a heap dump file, and then you can use "Jhat" (heap analysis tool) to analyze these heap dumps.

10 Essentials of Java Heap memory (heap memories)

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. To facilitate garbage collection, the Java heap space is divided into three zones, called New Generation, old Generation or tenured Generation, and perm space.

4. You can resize 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 indicate the unit. For example, you can use-xmx256m to set the maximum heap memory size to 256MB.

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 get heap dump and use "jhat" to analyze heap dump.

7. Java heap space is different from the stack space, and the stack space is used to store the call stack and local variables.

8. The Java garbage collector is used to reclaim memory consumed by dead objects (objects that are no longer in use) and then release them into the Java heap space.

9. When you encounter Java.lang.outOfMemoryError, don't be nervous, sometimes just increase the heap space can be, but if often appear, it is necessary to see whether there is a memory leak in the Java program.

10. Use the profiler and heap dump analysis tools to view the Java heap space to see how much memory is allocated to each object.

English: http://javarevisited.blogspot.com/2011/05/java-heap-space-memory-size-jvm.html

Original link: http://blog.jobbole.com/13373/

10 Essentials of Java Heap memory

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.