执行者内存不足

时间:2017-06-21 07:56:03

标签: android out-of-memory executorservice

我正在尝试扫描本地网络以查找具有特定端口的设备。

当我进行6-7次扫描时,我会记忆不足。

 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);

findIpAdressFunction:

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];
}

logcat错误:

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)

错误在于executorService bun我不知道为什么......

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

您正在为每个IP创建新的线程池。

您应该在for循环之外调用Executors.newCachedThreadPool()并将其结果存储在变量中。然后在循环内的变量上调用Execute

答案 1 :(得分:0)

我只需添加:executorService.shutdownNow();它已经解决了