Android 执行器内存不足
我正在尝试扫描我的本地网络,以查找具有特定打开端口的设备 当我扫描6-7次时,我会从内存中取出一个Android 执行器内存不足,android,out-of-memory,executorservice,Android,Out Of Memory,Executorservice,我正在尝试扫描我的本地网络,以查找具有特定打开端口的设备 当我扫描6-7次时,我会从内存中取出一个 WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE); int ipAddress = wifiManager.getConnectionInfo().getIpAddress(); @SuppressLint("DefaultLocale")
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
int ipAddress = wifiManager.getConnectionInfo().getIpAddress();
@SuppressLint("DefaultLocale") String localIp = String.format("%d.%d.%d.%d",
(ipAddress & 0xff),
(ipAddress >> 8 & 0xff),
(ipAddress >> 16 & 0xff),
(ipAddress >> 24 & 0xff));
String s = ServerUtilities.findServerIp(localIp);
FinDipAddress函数:
public static String findServerIp(String localIp) {
final String functionName = "findServerIp";
final String[] serverIp = {""};
long startTime = System.currentTimeMillis();
String prefix = localIp.substring(0, localIp.lastIndexOf(".") + 1);
ConfigManager.printLog(functionName, "Started", AppConfig.LOG_TYPE_DEBUG);
if (localIp.length() > 0) {
for (int i = 0; i < 255; i++) {
final String testIp = prefix + String.valueOf(i);
Executors.newCachedThreadPool().execute(new Runnable() {
@Override
public void run() {
try {
InetAddress inetAddress = InetAddress.getByName(testIp);
checkInetAddress(inetAddress);
} catch (ConnectException e) {
if (e.toString().contains("ECONNREFUSED"))
ConfigManager.printLog(functionName, "Wrong server : connection refused", AppConfig.LOG_TYPE_VERBOSE);
else
ConfigManager.printLog(functionName, "Error : " + e.toString() + "\n\tip=" + testIp, AppConfig.LOG_TYPE_ERROR);
} catch (IOException e) {
ConfigManager.printLog(functionName, "Error scanning network : " + e.toString() + "\n\tip=" + testIp, AppConfig.LOG_TYPE_ERROR);
}
}
private void checkInetAddress(InetAddress inetAddress) throws IOException {
if (inetAddress.isReachable(500)) {
ConfigManager.printLog(functionName, "Testing " + testIp, AppConfig.LOG_TYPE_DEBUG);
new Socket(testIp, AppConfig.SERVER_PORT_CLIENT);
new Socket(testIp, AppConfig.SERVER_PORT_DOWNLOAD);
serverIp[0] = testIp;
}
}
});
}
} else {
ConfigManager.printLog(functionName, "Error\n\tlocal IP is null or empty : " + localIp, AppConfig.LOG_TYPE_ERROR);
}
long endTime = System.currentTimeMillis();
ConfigManager.printLog(functionName,
"Task finished\n\tresult : \"" + serverIp[0] + "\"\n\telapsed time : " + String.valueOf(endTime - startTime) + " (ms)"
, AppConfig.LOG_TYPE_INFO);
return serverIp[0];
}
错误在于Executor服务包我不知道为什么
感谢您的帮助。您正在为每个IP创建一个新的线程池
您应该在for循环外部调用Executors.newCachedThreadPool(),并将其结果存储在变量中。然后对循环中的变量调用
Execute
。我只添加了:executorService.shutdownNow();解决了这个问题我也试过这样:`ExecutorService ExecutorService=Executors.newCachedThreadPool();如果(localIp.length()>0){for(inti=0;i<255;i++){final String testIp=prefix+String.valueOf(i);executorService.execute(new Runnable()){`另外,请使用一个固定大小的执行器,否则您将阻塞device@eduyayo+1.我以为newCachedThreadPool
可能有一个硬限制,但是这个线程表明了相反的情况
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.sanilea.speedclientserverside, PID: 23369
java.lang.OutOfMemoryError: pthread_create (stack size 16384 bytes) failed: Try again
at java.lang.VMThread.create(Native Method)
at java.lang.Thread.start(Thread.java:1029)
at java.util.concurrent.ThreadPoolExecutor.addWorker(ThreadPoolExecutor.java:920)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1338)
at com.sanilea.speedclientserverside.utility.ServerUtilities.findServerIp(ServerUtilities.java:163)
at com.sanilea.speedclientserverside.activities.ConnexionActivity.scanAvailableNetwork(ConnexionActivity.java:96)
at com.sanilea.speedclientserverside.activities.ConnexionActivity.access$000(ConnexionActivity.java:42)
at com.sanilea.speedclientserverside.activities.ConnexionActivity$1.onClick(ConnexionActivity.java:64)
at android.view.View.performClick(View.java:4508)
at android.view.View$PerformClick.run(View.java:18675)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5590)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)