Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the 10 main points of Java heap memory?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about the 10 key points of Java heap memory, which may not be understood by many people. In order to let everyone know more, Xiaobian summarized the following contents for everyone. I hope you can gain something according to this article.

When I started learning Java programming, I didn't know what heap memory or heap space was, and I didn't even know where objects were placed when they were created. When I started writing some programs, I often encountered java.lang.outOfMemoryError errors, and then I began to pay attention to what heap memory or heap space is. Most programmers go through this process, because learning a language is easy, but learning the basics is hard, because there is no specific process for you to learn every foundation of programming and discover the secrets of programming. It is important for programmers to know heap space, set heap space, handle heap space outOfMemoryError errors, and parse heap dumps. This tutorial on Java heap is for my brother who just started learning programming. If you know the basics or know what's going on underneath, of course it might not help that much. Unless you know that the object was created in the heap, you don't realize that OutOfMemoryError occurs in heap space. I have written down everything I know about heaps, and I hope you will contribute and share as much of your knowledge as possible so that others can benefit.

What is heap space in Java?

When a Java program starts running, the JVM fetches some memory from the operating system. The JVM uses this memory, part of which is heap memory. Heap memory is usually at the bottom of the storage address, arranged upward. When an object is created with the new keyword or otherwise, the object gets memory from the heap. When the object is no longer in use and is collected as garbage, the memory is returned to 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, the default heap size for Java is 128MB on Sun JVMs, but there are exceptions, such as on the 32-bit Solaris operating system (SPARC platform version), where the default ** heap size and starting heap size are-Xms=3670K and-Xmx=64M. For 64-bit operating systems, the average heap size increases by about 30%. But if you use Java 1.5's throughput garbage collector, the default heap size for *** is a quarter of physical memory, and the starting heap size is a sixteenth of physical memory. To find out the default heap size, open a program with the default settings, use JConsole(supported since JDK 1.5) to view it, and see the *** heap size on the VM Summary page.

This way you can vary the heap size according to the needs of your application, and I strongly recommend using this method instead of the default. If your program is large and has a lot of objects to create, you can change the heap size with the-Xms and -Xmx arguments. Xms represents the starting heap memory size and Xmx represents the heap memory size of ***. There is also a parameter-Xmn, which indicates the size of the new generation (mentioned later). One thing you should note is that you cannot arbitrarily change the size of heap memory; you can only set it at JVM startup.

Heap and garbage collection

We know that objects are created in heap memory, and garbage collection is a process that clears dead objects out of heap space and returns that memory to the heap. For use by garbage collectors, the heap is divided into three main areas, called New Generation, Old Generation or Tenured Generation, and Perm space. New Generation is the space used to store newly created objects and is used when objects are 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 class, method, string pool, and class-level details. You can check out "How garbage collection works in Java" for more information about heaps and garbage collection.

OutOfMemoryError error in Java heap

When the JVM starts, the memory pairs set with the-Xms parameter are used. As the program proceeds, creating more objects, the JVM begins to expand heap memory to accommodate more objects. The JVM also uses a garbage collector to reclaim memory. When you reach the *** heap memory set by-Xmx, if there is no more memory available to allocate to new objects, the JVM throws java.lang.outofmemoryerror and your program crashes. Before throwing OutOfMemoryError, the JVM tries to free up enough space with the garbage collector, but if it finds that there is still not enough space, it throws this error. To solve this problem, you need to know information about your program objects, such as which objects you create, which objects take up how much space, and so on. You can use profiler or heap analyzer to handle OutOfMemoryError errors. "java.lang.OutOfMemoryError: Java heap space" indicates that the heap does not have enough space to expand. "java.lang.OutOfMemoryError: PermGen space" indicates that the permanent generation is full and your program cannot be loaded into a class or allocated another string.

Java Heap dump

Heap dump is a snapshot of 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 tools in JDK that can help you get heap dumps, and 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, and then you can use "jhat"(heap analysis tool) to analyze these heap dumps.

10 Key Points of Java Heap Memory

1. Java heap memory is the portion of memory that the operating system allocates to the JVM.

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

3. To facilitate garbage collection, Java heap space is divided into three areas, called New Generation, Old Generation or Tenured Generation, and Perm Space.

4. You can resize Java heap space by using JVM command-line options-Xms, -Xmx, -Xmn. Don't forget to add an "M" or "G" after the size to indicate the unit. For example, you can use-Xmx256m to set the size of heap memory *** to 256MB.

5. You can use JConsole or Runtime.maxMemory(), Runtime.totalMemory(), Runtime.freeMemory() to see heap memory size in Java.

6. You can use the command "jmap" to get the heap dump and "jhat" to parse the heap dump.

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

8. Java garbage collector is used to collect memory occupied by dead objects (objects that are no longer in use) and release them into Java heap space.

9. When you encounter java.lang.outOfMemoryError, don't be nervous, sometimes just add heap space, but if it happens often, see if there is a memory leak in Java programs.

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

After reading the above, do you have any further understanding of the 10 points of Java heap memory? If you still want to know more knowledge or related content, please pay attention to the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report