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

How to find references to files that are not closed from JVM heap dump

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article will explain in detail how to find references to unclosed files from JVM heap dump. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Recently troubleshoot a file that has not been closed and record it.

It is easier to find which files are not closed, just look at the fd (File Descriptor) of the process. But determining where the fd is opened and where it is referenced is more complicated, especially if the application is not restarted.

In JVM, you can easily reverse-check the references of objects through heap dump to find the leaked code.

Take the following simple demo as an example, Demo creates a temporary file without close dropping:

one

two

three

four

five

six

seven

eight

nine

ten

eleven

Import java.io.File

Import java.io.FileInputStream

Import java.io.IOException

Public class Test {

Public static void main (String [] args) throws IOException {

File tempFile = File.createTempFile ("test", "ttt")

FileInputStream fi = new FileInputStream (tempFile)

System.in.read ()

}

}

Find the corresponding fd by file name

The file opened by the process has a corresponding fd (File Descriptor) in OS, which can be viewed with the lsof command or directly in the / proc directory under linux.

Taking demo as an example, the fd for finding the test file is 12:

one

two

three

four

five

six

seven

eight

$ls-alh / proc/11278/fd/

Total 0

Dr-x- 2 admin users 0 Jun 30 18:20.

Dr-xr-xr-x 8 admin users 0 Jun 30 18:20..

Lrwx- 1 admin users 64 Jun 30 18:20 0-> / dev/pts/0

Lrwx- 1 admin users 64 Jun 30 18:20 1-> / dev/pts/0

Lr-x- 1 admin users 64 Jun 30 18:24 11-> / dev/urandom

Lr-x- 1 admin users 64 Jun 30 18:24 12-> / tmp/test7607712940880692142ttt

Heap dump the process

Use the jmap command:

one

Jmap-dump:live,format=b,file=heap.bin 11278

Query java.io.FileDescriptor objects through OQL

For each open file, there is a java.io.FileDescriptor object in JVM. Looking at the source code, you can find that there is a fd field in FileDescriptor:

one

two

Public final class FileDescriptor {

Private int fd

So you need to find the FileDescriptor,QOL statement where fd equals 12:

one

Select s from java.io.FileDescriptor s where s.fd = = 12

Use the OQL console in VisualVM to query

After having VisualVM,jdk9 in jdk8, you can download it separately: https://visualvm.github.io/

Import the heap dump file into VisualVM, and then query the above statement in the "OQL console". The result is:

Then you can query the parent and reference the relevant objects.

Use jhat query

There are many other heap dump tools besides VisualVM, and there is a jhat tool that comes with jdk. Although it was removed after jdk9, I prefer this tool because it is a web interface.

If you visit http://localhost:7000/oql/, you can query OQL in the browser:

Open the link to view specific information

First find the fd that did not close the file.

Find out the corresponding java.io.FileDescriptor object from heap dump according to fd, and then find the relevant references.

On how to find references to unclosed files from JVM heap dump to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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

Servers

Wechat

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

12
Report