JMap and JStack Tutorial

28/12/2020

For any java delopers Jmap and Jstack are two important utility programs that can be used to debug issues, or understand what your java program is doing.  They are critical tools in any your toolbox that you need to know about.  Therefore in this tutorial I will explain both Jstack and Jmap so you can get the idea.  Let’s get started:

Troubleshooting with Jstack

Jstack is a troubleshooting tool in java that generates thread dumps of a particular JVM process, remote debug server or Java core file. This means that from whichever Java frame you use, there will be a full class name, bytecode index, method name, and the line number. Since the Jstack tool mainly deals with diagnostics, you can quickly see what the code is doing when running live.

This is among the most critical roles of the Jstack utility. Basically, it is a command line utility that attaches itself to a particular core file or process and returns all consequent threads attached to the JVM, including all the virtual machine’s internal threads and even the original stack frames. What’s more, this tool can also be used to detect deadlocks in an application so that debugging can be a bit easier. This is the best option if you are looking for a complete app diagnosis to resolve issues such as hangs and deadlocks.

Forcing a Stack Dump

In case the system is not responding due to a hung process, getting a stack dump might be the only way to tell the origin of the problem and how to fix it. In the event that the Jstack process ID is not responding, the –F option can come in handy.  Here is a screenshot of a force dump:

Stack traces from core dumps

A core dump is simply a system or memory dump that has a record of the state of a particular file or application at a particular time and mostly when there was a problem that caused the app or program to terminate unexpectedly. Core dumps can be rigorous and have extensive information on the application. However, sometimes we only require the stack traces. To do so, we need to use the Jstack command as shown below on a core file.

$ jstack $JAVA_HOME/bin/java core

Mixed Stack

Sometimes you need more than the java stacks to identify a problem in a java application. When you need both the native stack frames and the Java stacks, the Jstack utility can be of great assistance. These native frames are frames created with C/C++. To do this, you only need the –m command as shown in the progressive screenshots below:

Jmap utility

Apps run into many issues during development. Sometimes there are memory issues such as an ever-expanding heap that never reduces despite regular garbage collection or an ArrayList which is not being released when it should. As such, we need to identify the source of the leak and patch it as soon as possible. To this end, we have the Jmap utility that takes a snapshot of the whole heap. Here are a few ways the Jmap command is used.

Heap Configuration

The –heap command is used to gather the following heap information:

Data specific to the garbage collection (GC) algorithms such as the name of the algorithm, e.g. serial GC and the fine print such as the number of threads for serial garbage collection, etc.

Heap usage reports: for each section of the heap (generation), the tool provides information such as total heap capacity and the memory available at the moment. Here is a screenshot of the output from the command:

Heap histogram

Still using the jmap utility, you can get the histogram of a heap by using the –histo option. Using the jmap –histo command, you can print the histogram of a particular core file or running process. When working on a running process, the tool prints out the memory size in bytes, number of objects and the class name. Here is a sample output of the jmap –histo command on a process with PID 29665:

Permanent Generation Statistics

The permanent generation is the sector of the heap that houses all the relevant info about the virtual machine itself. This information includes method and class objects that show where everything is. This configuration is essential especially for applications that generate huge loads for the classes present. For every loader object, we have the following details displayed:

  • The total number of loaded classes
  • Address of the class loader at the exact moment the utility was run.
  • The number of consumed bytes by the class loader.
  • If there is a parent class loader, its address is also recorded.
  • A live or dead sign as to whether the loader object will be collected as garbage in future.

Sources

http://www.logicbig.com/quick-info/java/jmap/
http://www.tutorialspoint.com/unix_commands/jstack.htm
https://blog.gceasy.io/2015/08/14/how-to-capture-heap-dump-jmap/
https://www.ibm.com/developerworks/library/j-5things8/index.html
https://docs.oracle.com/javase/8/docs/technotes/tools/unix/jstack.html
https://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/tooldescr014.html

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Install Oracle JDK 10 on Arch Linux

Oracle JDK is used by Java developers all over the world to develop, test Java applications. Oracle released version 10...
28/12/2020

Eclipse Java Tutorial

Eclipse Integrated Development Environment is an incredible open source compilation of tools, projects and co-existing working...
28/12/2020

Install OpenJDK 8 on Debian 9 Stretch

Java Development Kit or JDK is used by Java programmers all over the world. Java is a compiled language. But unlike C/C++,...
28/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

SỰ KHÁC BIỆT GIỮA RESIDENTIAL PROXY VÀ PROXY DATACENTER
17/02/2024

Mua Proxy v6 US Private chạy PRE, Face, Insta, Gmail
07/01/2024

Mua shadowsocks và hướng dẫn sữ dụng trên window
05/01/2024

Tại sao Proxy Socks lại được ưa chuộng hơn Proxy HTTP?
04/01/2024

Mua thuê proxy v4 nuôi zalo chất lượng cao, kinh nghiệm tránh quét tài khoản zalo
02/01/2024