记一次 JAVA 的内存泄露分析
码农突围
共 6517字,需浏览 14分钟
·
2021-07-18 15:32
背景
jmap -histo:live `pid of java`
# 为了便于观察,还是将输出写入文件
jmap -histo:live `pid of java` > /tmp/jmap00
jmap -dump:format=b,file=heap.dump `pid of java
离线分析
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpasyncclient</artifactId>
<version>4.1.3</version>
</dependency>
public class HttpAsyncClient {
private CloseableHttpAsyncClient httpclient;
public HttpAsyncClient() {
httpclient = HttpAsyncClients.createDefault();
httpclient.start();
}
public void execute(HttpUriRequest request, FutureCallback<HttpResponse> callback){
httpclient.execute(request, callback);
}
public void close() throws IOException {
httpclient.close();
}
}
主要逻辑:
public class ReplayApplication {
public static void main(String[] args) throws InterruptedException {
//创建有内存泄露的回放客户端
ReplayWithProblem replay1 = new ReplayWithProblem();
//加载一万条请求数据放入缓存
List<HttpUriRequest> cache1 = replay1.loadMockRequest(10000);
//开始循环回放
replay1.start(cache1);
}
}
回放客户端实现(内存泄露):
public class ReplayWithProblem {
public List<HttpUriRequest> loadMockRequest(int n){
List<HttpUriRequest> cache = new ArrayList<HttpUriRequest>(n);
for (int i = 0; i < n; i++) {
HttpGet request = new HttpGet("http://www.baidu.com?a="+i);
cache.add(request);
}
return cache;
}
public void start(List<HttpUriRequest> cache) throws InterruptedException {
HttpAsyncClient httpClient = new HttpAsyncClient();
int i = 0;
while (true){
final HttpUriRequest request = cache.get(i%cache.size());
httpClient.execute(request, new FutureCallback<HttpResponse>() {
public void completed(final HttpResponse response) {
System.out.println(request.getRequestLine() + "->" + response.getStatusLine());
}
public void failed(final Exception ex) {
System.out.println(request.getRequestLine() + "->" + ex);
}
public void cancelled() {
System.out.println(request.getRequestLine() + " cancelled");
}
});
i++;
Thread.sleep(100);
}
}
}
内存分析:
1.启动情况:
代码优化
public class ReplayApplication {
public static void main(String[] args) throws InterruptedException {
ReplayWithoutProblem replay2 = new ReplayWithoutProblem();
List<String> cache2 = replay2.loadMockRequest(10000);
replay2.start(cache2);
}
}
public class ReplayWithoutProblem {
public List<String> loadMockRequest(int n){
List<String> cache = new ArrayList<String>(n);
for (int i = 0; i < n; i++) {
cache.add("http://www.baidu.com?a="+i);
}
return cache;
}
public void start(List<String> cache) throws InterruptedException {
HttpAsyncClient httpClient = new HttpAsyncClient();
int i = 0;
while (true){
String url = cache.get(i%cache.size());
final HttpGet request = new HttpGet(url);
httpClient.execute(request, new FutureCallback<HttpResponse>() {
public void completed(final HttpResponse response) {
System.out.println(request.getRequestLine() + "->" + response.getStatusLine());
}
public void failed(final Exception ex) {
System.out.println(request.getRequestLine() + "->" + ex);
}
public void cancelled() {
System.out.println(request.getRequestLine() + " cancelled");
}
});
i++;
Thread.sleep(100);
}
}
}
结果验证
总结
- END - 最近热文
• 新来的妹纸rm -rf把公司整个数据库删没了,整个项目组慌了~ • 2021年7月程序员工资统计,平均15302元 • 那些学计算机的女生后来都怎么样了? • 字节跳动取消大小周,员工却高兴不起来!内网哀嚎:变相降薪20%,少赚一万!
评论