我试图优化(内存)我的程序,但 GC 仍然使其滞后



我用Java编写了一个软件,通过使用代理发送HTTP请求来检查代理是否正常工作。

它从数据库中获取大约 30,000 个代理,然后尝试检查它们是否正常运行。从数据库收到的代理曾经作为ArrayList<String>返回,但由于以下原因已更改为Deque<String>

程序的工作方式是有一个ProxyRequest对象,分别将IP和端口存储为字符串和int。ProxyRequest对象有一个方法isWorkingProxy()该方法尝试使用代理发送请求并返回有关请求是否成功的boolean

ProxyRequest对象由一个RunnableProxyRequest对象环绕,该对象在重写的run()方法中调用super.isWorkingProxy()。根据super.isWorkingProxy()的响应,RunnableProxyRequest对象更新一个MySQL数据库。

请注意,MySQL 数据库的更新是synchronized()

它使用 FixedThreadPool(在 VPS 上(在 750 个线程上运行,但朝向 最后,它变得非常慢(卡在~50个线程上(,这显然 暗示垃圾回收器正在工作。这就是问题所在。

我尝试了以下方法来改善滞后,它似乎不起作用:

1( 使用Deque<String>代理并使用Deque.pop()获取代理所在的String。这(我相信(不断使Deque<String>变小,这应该可以改善GC引起的滞后。

2( 设置con.setConnectTimeout(this.timeout);,其中this.timeout = 5000;这样,连接应在 5 秒内返回结果。否则,线程已完成,线程池中不应再处于活动状态。

除此之外,我不知道任何其他方法可以提高性能。

任何人都可以为我推荐一种提高性能以避免/停止 GC 滞后到线程末尾的方法吗?我知道有一个关于这个问题的 Stackoverflow 问题(Java 线程在处理结束时变慢(,但我已经尝试了答案中的所有内容,但它对我不起作用。

谢谢你的时间。

代码片段:

循环将线程添加到FixedThreadPool

//This code is executed recursively (at the end, main(args) is called again)
//Create the threadpool for requests
//Threads is an argument that is set to 750.
ThreadPoolExecutor executor = (ThreadPoolExecutor)Executors.newFixedThreadPool(threads);
Deque<String> proxies = DB.getProxiesToCheck();
while(proxies.isEmpty() == false) {
try {
String[] split = proxies.pop().split(":");
Runnable[] checks = new Runnable[] {
//HTTP check
new RunnableProxyRequest(split[0], split[1], Proxy.Type.HTTP, false),
//SSL check
new RunnableProxyRequest(split[0], split[1], Proxy.Type.HTTP, true),
//SOCKS check
new RunnableProxyRequest(split[0], split[1], Proxy.Type.SOCKS, false)
//Add more checks to this list as time goes...
};
for(Runnable check : checks) {
executor.submit(check);
}
} catch(IndexOutOfBoundsException e) {
continue;
}
}

ProxyRequest类:

//Proxy details
private String proxyIp;
private int proxyPort;
private Proxy.Type testingType;
//Request details
private boolean useSsl;
public ProxyRequest(String proxyIp, String proxyPort, Proxy.Type testingType, boolean useSsl) {
this.proxyIp = proxyIp;
try {
this.proxyPort = Integer.parseInt(proxyPort);
} catch(NumberFormatException e) {
this.proxyPort = -1;
}
this.testingType = testingType;
this.useSsl = useSsl;
}
public boolean isWorkingProxy() {
//Case of an invalid proxy
if(proxyPort == -1) {
return false;
}
HttpURLConnection con = null;
//Perform checks on URL
//IF any exception occurs here, the proxy is obviously bad.
try {
URL url = new URL(this.getTestingUrl());
//Create proxy
Proxy p = new Proxy(this.testingType, new InetSocketAddress(this.proxyIp, this.proxyPort));
//No redirect
HttpURLConnection.setFollowRedirects(false);
//Open connection with proxy
con = (HttpURLConnection)url.openConnection(p);
//Set the request method
con.setRequestMethod("GET");
//Set max timeout for a request.
con.setConnectTimeout(this.timeout);
} catch(MalformedURLException e) {
System.out.println("The testing URL is bad. Please fix this.");
return false;
} catch(Exception e) {
return false;
}
try(
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
) {
String inputLine = null; StringBuilder response = new StringBuilder();
while((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
//A valid proxy!
return con.getResponseCode() > 0;
} catch(Exception e) {
return false;
}
}

RunnableProxyRequest类:

public class RunnableProxyRequest extends ProxyRequest implements Runnable {

public RunnableProxyRequest(String proxyIp, String proxyPort, Proxy.Type testingType, boolean useSsl) {
super(proxyIp, proxyPort, testingType, useSsl);
}
@Override
public void run() {
String test = super.getTest();
if(super.isWorkingProxy()) {
System.out.println("-- Working proxy: " + super.getProxy() + " | Test: " +  test);
this.updateDB(true, test);
} else {
System.out.println("-- Not working: " + super.getProxy() + " | Test: " +  test);
this.updateDB(false, test);
}   

}
private void updateDB(boolean success, String testingType) {
switch(testingType) {
case "SSL":
DB.updateSsl(super.getProxyIp(), super.getProxyPort(), success);
break;
case "HTTP":
DB.updateHttp(super.getProxyIp(), super.getProxyPort(), success);
break;
case "SOCKS":
DB.updateSocks(super.getProxyIp(), super.getProxyPort(), success);
break;
default:
break;
}
}
}

DB类:

//Locker for async 
private static Object locker = new Object();
private static void executeUpdateQuery(String query, String proxy, int port, boolean toSet) {
synchronized(locker) {
//Some prepared statements here.
}
}

感谢彼得劳里指导我找到解决方案! :)
他的评论:

@ILoveKali 我发现网络库在 当事情真的出错时关闭连接。超时倾向 以便在连接正常时最佳工作。扬子晚报

所以我做了一些研究,发现我也不得不使用该方法setReadTimeout(this.timeout);。以前,我只使用setConnectTimeout(this.timeout);

感谢这篇文章(HttpURLConnection超时默认值(解释了以下内容:

不幸的是,根据我的经验,使用这些默认值似乎可以 导致不稳定状态,具体取决于您的情况 与服务器的连接。如果您使用 HttpURL 通信并且不使用 显式设置(至少读取(超时,您的连接可以进入 永久陈旧状态。默认情况下。所以总是将 setReadTimeout 设置为 "某物",或者您可能孤立连接(可能还有线程 具体取决于应用的运行方式(。

所以最终的答案是:GC做得很好,它不对滞后负责。线程只是永远停留在单个数字上,因为我没有设置读取超时,因此isWorkingProxy()方法从未得到结果并继续读取。

最新更新