Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

启用StrictMode后,检测到有问题 #6

Open
ghost opened this issue Oct 18, 2016 · 5 comments
Open

启用StrictMode后,检测到有问题 #6

ghost opened this issue Oct 18, 2016 · 5 comments

Comments

@ghost
Copy link

ghost commented Oct 18, 2016

strictmode 设置如下:

StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads()
.detectDiskWrites()
.detectNetwork()
.penaltyLog()
.build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects()
.detectLeakedClosableObjects()
.penaltyLog()
.penaltyDeath()
.build());

下面是strictmode 输出的信息,这块儿代码仅仅是修改了包名。

10-18 11:29:12.095 17675-17685/com.beibo.yuerbao E/StrictMode: A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
java.lang.Throwable: Explicit termination method 'close' not called
at dalvik.system.CloseGuard.open(CloseGuard.java:180)
at java.net.AbstractPlainSocketImpl.create(AbstractPlainSocketImpl.java:103)
at java.net.Socket.createImpl(Socket.java:451)
at java.net.Socket.connect(Socket.java:584)
at com.husor.dns.dnscache.speedtest.impl.Socket80Test.speedTest(Socket80Test.java:20)
at com.husor.dns.dnscache.speedtest.SpeedtestManager.speedTest(SpeedtestManager.java:58)
at com.husor.dns.dnscache.DNSCache$SpeedTestTask.updateSpeedInfo(DNSCache.java:359)
at com.husor.dns.dnscache.DNSCache$SpeedTestTask.run(DNSCache.java:346)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)

@ghost
Copy link
Author

ghost commented Feb 14, 2017

@Override public int speedTest(String ip, String host) { Socket socket = null; try { long begin = System.currentTimeMillis(); Socket s1 = new Socket(); s1.connect(new InetSocketAddress(ip, 80), TIMEOUT); long end = System.currentTimeMillis(); int rtt = (int) (end - begin); return rtt; } catch (Exception e) { e.printStackTrace(); } finally { try { if (null != socket) { socket.close(); } } catch (IOException e) { e.printStackTrace(); } } return SpeedtestManager.OCUR_ERROR; }
应该是没有close s1导致的

@JackyAndroid
Copy link

@huangyanan 解决了吗?说下方案

@ghost
Copy link
Author

ghost commented Sep 8, 2017

@JackyAndroid 解决了,关注一下那个socket 对象的初始化和关闭,初始化的socket并没有被关闭。

@JackyAndroid
Copy link

@huangyanan
改后如下,请确认下是否正确,另外修改Socket80Test影响范围有多大?

public class Socket80Test extends BaseSpeedTest {

    static final int TIMEOUT = 5 * 1000;

    @Override
    public int speedTest(String ip, String host) {
        Socket socket = null;
        try {
            long begin = System.currentTimeMillis();
            socket = new Socket();
            socket.connect(new InetSocketAddress(ip, 80), TIMEOUT);
            long end = System.currentTimeMillis();
            int rtt = (int) (end - begin);
            return rtt;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != socket) {
                    socket.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return SpeedtestManager.OCUR_ERROR;
    }

    @Override
    public int getPriority() {
        return 10;
    }

    @Override
    public boolean isActivate() {
        return true;
    }
}

@ghost
Copy link
Author

ghost commented Sep 8, 2017

无发现异常影响,我这边线上已经使用修改一年了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant