Home Java javaTutorial Share ten key points of Java heap memory

Share ten key points of Java heap memory

Apr 26, 2017 am 10:11 AM
java Memory

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Square Root in Java Square Root in Java Aug 30, 2024 pm 04:26 PM

Guide to Square Root in Java. Here we discuss how Square Root works in Java with example and its code implementation respectively.

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Random Number Generator in Java Random Number Generator in Java Aug 30, 2024 pm 04:27 PM

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Armstrong Number in Java Armstrong Number in Java Aug 30, 2024 pm 04:26 PM

Guide to the Armstrong Number in Java. Here we discuss an introduction to Armstrong's number in java along with some of the code.

Sources say Samsung Electronics and SK Hynix will commercialize stacked mobile memory after 2026 Sources say Samsung Electronics and SK Hynix will commercialize stacked mobile memory after 2026 Sep 03, 2024 pm 02:15 PM

According to news from this website on September 3, Korean media etnews reported yesterday (local time) that Samsung Electronics and SK Hynix’s “HBM-like” stacked structure mobile memory products will be commercialized after 2026. Sources said that the two Korean memory giants regard stacked mobile memory as an important source of future revenue and plan to expand "HBM-like memory" to smartphones, tablets and laptops to provide power for end-side AI. According to previous reports on this site, Samsung Electronics’ product is called LPWide I/O memory, and SK Hynix calls this technology VFO. The two companies have used roughly the same technical route, which is to combine fan-out packaging and vertical channels. Samsung Electronics’ LPWide I/O memory has a bit width of 512

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

See all articles