java.lang.OutOfMemoryError: Metaspace
is a difficult error to diagnose. On this put up, we’ll delve into the basis causes behind this error, discover potential options, and focus on efficient diagnostic strategies to troubleshoot this downside. Let’s equip ourselves with the data and instruments to overcome this widespread adversary.JVM Reminiscence Areas
To higher perceive OutOfMemoryError
, we first want to grasp totally different JVM Reminiscence areas. Here’s a video clip that offers a superb introduction to totally different JVM reminiscence areas. However in a nutshell, JVM has the next reminiscence areas:
- Younger Era: Newly created utility objects are saved on this area.
- Previous Era: Software objects which might be residing for an extended period are promoted from the Younger Era to the Previous Era. Principally, this area holds long-lived objects.
- Metaspace: Class definitions, technique definitions, and different metadata which might be required to execute your program are saved within the Metaspace area. This area was added in Java 8. Earlier than that metadata definitions have been saved within the PermGen. Since Java 8, PermGen was changed by Metaspace.
- Threads: Every utility thread requires a thread stack. House allotted for thread stacks, which comprise technique name info and native variables are saved on this area.
- Code cache: Reminiscence areas the place compiled native code (machine code) of strategies is saved for environment friendly execution are saved on this area.
- Direct buffer: ByteBuffer objects are utilized by fashionable frameworks (i.e., Spring WebClient) for environment friendly I/O operations. They’re saved on this area.
- GC (Rubbish Assortment): Reminiscence required for computerized rubbish assortment to work is saved on this area.
- JNI (Java Native Interface): Reminiscence for interacting with native libraries and code written in different languages is saved on this area.
- misc: There are areas particular to sure JVM implementations or configurations, equivalent to the inner JVM buildings or reserved reminiscence areas, they’re categorised as ‘misc’ areas.
What Is java.lang.OutOfMemoryError: Metaspace?
With plenty of class definitions, technique definitions are created within the Metaspace
area than the allotted Metaspace reminiscence restrict (i.e., -XX:MaxMetaspaceSize
), JVM will throw java.lang.OutOfMemoryError: Metaspace
.
What Causes java.lang.OutOfMemoryError: Metaspace?
java.lang.OutOfMemoryError: Metaspace
is triggered by the JVM below the next circumstances:
- Creating a lot of dynamic courses: In case your utility makes use of Groovy type of scripting languages or Java Reflection to create new courses at runtime
- Loading a lot of courses: Both your utility itself has plenty of courses or it makes use of plenty of 3rd celebration libraries/frameworks which have plenty of courses in it.
- Loading a lot of class loaders: Your utility is loading plenty of class loaders.
Options for OutOfMemoryError: Metaspace
The next are the potential options to repair this error:
- Enhance Metaspace dimension: If
OutOfMemoryError
surfaced as a consequence of a rise within the variety of courses loaded, then elevated the JVM’s Metaspace dimension (-XX:MetaspaceSize
and-XX:MaxMetaspaceSize
). This answer is adequate to repair a lot of theOutOfMemoryError: Metaspace
errors, as a result of reminiscence leaks not often occur within the Metaspace area.
- Repair reminiscence leak: Analyze reminiscence leaks in your utility utilizing the method given on this put up. Be sure that class definitions are correctly dereferenced when they’re now not wanted to permit them to be rubbish collected.
Pattern Program That Generates OutOfMemoryError: Metaspace
To higher perceive java.lang.OutOfMemoryError: Metaspace
, let’s attempt to simulate it. Let’s leverage BuggyApp, a easy open-source chaos engineering challenge. BuggyApp can generate numerous kinds of efficiency issues equivalent to Reminiscence Leak, Thread Leak, Impasse, a number of BLOCKED
threads, and so on. Under is the Java program from the BuggyApp challenge that simulates java.lang.OutOfMemoryError: Metaspace
when executed.
import java.util.UUID;
import javassist.ClassPool;
public class OOMMetaspace {
public static void major(String[] args) throws Exception {
ClassPool classPool = ClassPool.getDefault();
whereas (true) {
// Maintain creating courses dynamically!
String className = "com.buggyapp.MetaspaceObject" + UUID.randomUUID();
classPool.makeClass(className).toClass();
}
}
}
Within the above program, the OOMMetaspace’ class’s ‘major()
technique accommodates an infinite whereas (true)
loop. Throughout the loop, the thread makes use of open-source library javassist to create dynamic courses whose names begin with com.buggyapp.MetaspaceObject
. Class names generated by this program will look one thing like this: com.buggyapp.MetaspaceObjectb7a02000-ff51-4ef8-9433-3f16b92bba78
. When so many such dynamic courses are created, the Metaspace reminiscence area will attain its restrict and the JVM will throw java.lang.OutOfMemoryError: Metaspace
.
How To Troubleshoot OutOfMemoryError: Metaspace
To diagnose OutOfMemoryError: Metaspace
, we have to examine the contents of the Metaspace area. Upon inspecting the contents, you may determine the leaking space of the appliance code. Here’s a weblog put up that describes a number of totally different approaches to inspecting the contents of the Metaspace area. You may select the method that fits your necessities. My favourite choices are:
1. -verbose:class
If you’re working on Java model 8 or under, then you should use this selection. Once you go the -verbose:class
choice to your utility throughout startup, it’ll print all of the courses which might be loaded into reminiscence. Loaded courses can be printed in the usual error stream (i.e., console, in the event you aren’t routing your error stream to a log file).
Instance:
java {app_name} -verbose:class
Once we handed the -verbose:class
flag to the above program, within the console we began to see the next strains to be printed:
[Loaded com.buggyapp.MetaspaceObjecta97f62c5-0f71-4702-8521-c312f3668f47 from
__JVM_DefineClass__]
[Loaded com.buggyapp.MetaspaceObject70967d20-609f-42c4-a2c4-b70b50592198 from
__JVM_DefineClass__]
[Loaded com.buggyapp.MetaspaceObjectf592a420-7109-42e6-b6cb-bc5635a6024e from
__JVM_DefineClass__]
[Loaded com.buggyapp.MetaspaceObjectdc7d12ad-21e6-4b17-a303-743c0008df87 from
__JVM_DefineClass__]
[Loaded com.buggyapp.MetaspaceObject01d175cc-01dd-4619-9d7d-297c561805d5 from
__JVM_DefineClass__]
[Loaded com.buggyapp.MetaspaceObject5519bef3-d872-426c-9d13-517be79a1a07 from
__JVM_DefineClass__]
[Loaded com.buggyapp.MetaspaceObject84ad83c5-7cee-467b-a6b8-70b9a43d8761 from
__JVM_DefineClass__]
[Loaded com.buggyapp.MetaspaceObject35825bf8-ff39-4a00-8287-afeba4bce19e from
__JVM_DefineClass__]
[Loaded com.buggyapp.MetaspaceObject665c7c09-7ef6-4b66-bc0e-c696527b5810 from
__JVM_DefineClass__]
[Loaded com.buggyapp.MetaspaceObject793d8aec-f2ee-4df6-9e0f-5ffb9789459d from
__JVM_DefineClass__]
: :
This can be a clear indication that courses with the com.buggyapp.MetaspaceObject
prefixes are loaded so steadily into the reminiscence. This can be a nice clue/trace to let you realize the place the leak is occurring within the utility.
2. -Xlog:class+load
If you’re working on Java model 9 or above, then you should use this selection. Once you go the -Xlog:class+load
choice to your utility throughout startup, it’ll print all of the courses which might be loaded into reminiscence. Loaded courses can be printed within the file path you’ve configured.
Instance:
java {app_name} -Xlog:class+load=data:/choose/log/loadedClasses.txt
If you’re nonetheless unable to find out the origination of the leak based mostly on the category title, then you are able to do a deep dive by taking a heap dump from the appliance. You can seize a heap dump utilizing one of many 8 choices mentioned on this put up. You would possibly select the choice that matches your wants. As soon as a heap dump is captured, it’s essential to use instruments like HeapHero, JHat, and so on. to research the dumps.
What Is Heap Dump?
Heap Dump is mainly a snapshot of your utility reminiscence. It accommodates detailed details about the objects and knowledge buildings current within the reminiscence. It’ll inform what objects are current within the reminiscence, whom they’re referencing, who’s referencing, what’s the precise buyer knowledge saved in them, what dimension of they occupy, whether or not they’re eligible for rubbish assortment, and so on. They supply precious insights into the reminiscence utilization patterns of an utility, serving to builders determine and resolve memory-related points.
How To Analyze Metaspace Reminiscence Leak By Heap Dump
HeapHero is out there in two modes:
- Cloud: You may add the dump to the HeapHero cloud and see the outcomes.
- On-Prem: You may register right here, get the HeapHero put in in your native machine, after which do the evaluation.
Be aware: I choose utilizing the on-prem set up of the device as an alternative of utilizing the cloud version as a result of heap dump tends to comprise delicate info (equivalent to SSN, Credit score Card Numbers, VAT, and so on.), and I don’t need the dump to be analyzed in exterior places.
As soon as the heap dump is captured, from the above program, we add it to the HeapHero device. The device analyzed the dump and generated the report. Within the report go to the ‘Histogram’ view. This view will present all of the courses which might be loaded into the reminiscence. On this view, you’ll discover the courses with the prefix com.buggyapp.MetaspaceObject
. Proper-click on the …
that’s subsequent to the category title. Then click on on the Listing Object(s) with > incoming references
as proven within the under determine.
When you do it, the device will show all of the incoming references of this explicit class. This may present the origin level of those courses as proven within the under determine. It’ll clearly present which a part of the code is creating these class definitions. As soon as we all know which a part of the code is creating these class definitions, then it will likely be straightforward to repair the issue.
Video Abstract
Right here’s a video abstract of the article:
Conclusion
On this put up, we’ve lined a spread of matters, from understanding JVM reminiscence areas to diagnosing and resolving java.lang.OutOfMemoryError: Metaspace
. We hope you’ve discovered the data helpful and insightful. However our dialog doesn’t finish right here. Your experiences and insights are invaluable to us and to your fellow readers. We encourage you to share your encounters with java.lang.OutOfMemoryError: Metaspace
within the feedback under. Whether or not it’s a singular answer you’ve found, a greatest follow you swear by, and even only a private anecdote, your contributions can enrich the training expertise for everybody.