gpt4 book ai didi

java - Apache HttpAsyncClient 中的文件描述符泄漏

转载 作者:行者123 更新时间:2023-12-01 12:55:15 29 4
gpt4 key购买 nike

在 Linux 上运行时(仅在 Linux 上尝试过),根据 lsof,以下代码似乎泄漏了 1 个 anon_inode 和 2 个管道文件描述符:

CloseableHttpAsyncClient client = HttpAsyncClients.createDefault();
client.start();
client.close();

但是,如果 client.execute() 被调用一次或多次,则不会发生泄漏。

CloseableHttpAsyncClient client = HttpAsyncClients.createDefault();
client.start();
client.execute(new HttpGet("http://stackoverflow.com/questions/23966483"), null);
client.close();
<小时/>

完整示例代码:

import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClients;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.management.ManagementFactory;
import java.util.concurrent.ExecutionException;

public class Benchmark {

public static void main(String[] args) throws ExecutionException, InterruptedException, IOException {
printFileDescriptorCounts();
for(int i = 0; i < 20; i++) {
CloseableHttpAsyncClient client = HttpAsyncClients.createDefault();
client.start();
//client.execute(new HttpGet("http://stackoverflow.com/questions/23966483"), null);
client.close();
printFileDescriptorCounts();
}
}

// not portable -- Linux only
private static void printFileDescriptorCounts() throws IOException {
String processId = ManagementFactory.getRuntimeMXBean().getName().split("@")[0];
String cmd = String.format("lsof -p %s | grep 'anon_inode\\|pipe' | awk '{ print $NF; }' | sort | uniq -c", processId);
Process process = Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", cmd});
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
try {
for(String line; (line = br.readLine()) != null; ) {
System.out.print(line.replace("\n", "\t"));
}
System.out.println();
} finally {
br.close();
process.destroy();
}
}

}

我明白了

  3 pipe
1 anon_inode 5 pipe
2 anon_inode 7 pipe
3 anon_inode 9 pipe
4 anon_inode 11 pipe
5 anon_inode 13 pipe
6 anon_inode 15 pipe
7 anon_inode 17 pipe
8 anon_inode 19 pipe
9 anon_inode 21 pipe
10 anon_inode 23 pipe
11 anon_inode 25 pipe
12 anon_inode 27 pipe
13 anon_inode 29 pipe
14 anon_inode 31 pipe
15 anon_inode 33 pipe
16 anon_inode 35 pipe
17 anon_inode 37 pipe
18 anon_inode 39 pipe
19 anon_inode 41 pipe
20 anon_inode 43 pipe

如果我取消注释 execute 行,泄漏就会消失。

  3 pipe
3 pipe
3 pipe
3 pipe
3 pipe
3 pipe
3 pipe
3 pipe
3 pipe
3 pipe
3 pipe
3 pipe
3 pipe
3 pipe
3 pipe
3 pipe
3 pipe
3 pipe
3 pipe
3 pipe
3 pipe
<小时/>

(1) 这是预期/预期的行为,还是一个错误?

(2) 为什么/如何发生这种情况?

最佳答案

看起来像是 I/O react 器状态管理逻辑中的缺陷。请为该项目筹集 JIRA。

https://issues.apache.org/jira/browse/HTTPCORE

关于java - Apache HttpAsyncClient 中的文件描述符泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23966483/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com