search
HomeJavajavaTutorialShare ten key points of Java heap memory
Share ten key points of Java heap memoryApr 26, 2017 am 10:11 AM
javaMemory

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
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
带你搞懂Java结构化数据处理开源库SPL带你搞懂Java结构化数据处理开源库SPLMay 24, 2022 pm 01:34 PM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

Java集合框架之PriorityQueue优先级队列Java集合框架之PriorityQueue优先级队列Jun 09, 2022 am 11:47 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

完全掌握Java锁(图文解析)完全掌握Java锁(图文解析)Jun 14, 2022 am 11:47 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

一起聊聊Java多线程之线程安全问题一起聊聊Java多线程之线程安全问题Apr 21, 2022 pm 06:17 PM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

详细解析Java的this和super关键字详细解析Java的this和super关键字Apr 30, 2022 am 09:00 AM

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

Java基础归纳之枚举Java基础归纳之枚举May 26, 2022 am 11:50 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

java中封装是什么java中封装是什么May 16, 2019 pm 06:08 PM

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

归纳整理JAVA装饰器模式(实例详解)归纳整理JAVA装饰器模式(实例详解)May 05, 2022 pm 06:48 PM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于设计模式的相关问题,主要将装饰器模式的相关内容,指在不改变现有对象结构的情况下,动态地给该对象增加一些职责的模式,希望对大家有帮助。

See all articles

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 agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version