简体   繁体   English

内存在哪里泄漏?

[英]Where is the memory leaking?

I'm trying to download the source code of a page. 我正在尝试下载页面的源代码。 But when I'm running the code it is running for eternity and the log is showing something like that forever. 但是,当我运行代码时,它将永久运行,并且日志将永远显示类似的内容。

GC_FOR_ALLOC freed 274K, 13% free 6863K/7815K, paused 0ms GC_FOR_ALLOC释放274K,13%释放6863K / 7815K,暂停0ms

Here is my code : 这是我的代码:

class downloadSource extends AsyncTask<String, Void, String>{

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        String line = "";
        int x;
        try {
            URL url = new URL(params[0]);
            InputStream inputStream = url.openStream();
            InputStreamReader reader = new InputStreamReader(inputStream);
            x = reader.read();
            while (x != -1) {
                char c = (char) x;
                x = reader.read();
                line += c;

            }
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return line;
    }

And I'm calling it like that from the OnCreate method : 我从OnCreate方法中这样称呼它:

    downloadSource task2=new downloadSource();
    String source = "";
    try {
         source = task2.execute("https://www.imdb.com/list/ls052283250/").get();
    } catch (ExecutionException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

Please help me ! 请帮我 ! Thanks 谢谢

Your problem is probably not related to a memory leak. 您的问题可能与内存泄漏无关。 While your approach to read the stream is inefficient as mentioned by Holger , it is also not your main problem. 正如Holger所述 ,虽然读取流的方法效率低下,但这也不是您的主要问题。

Try adding the following line as the first line in your onCreate: 尝试将以下行添加为onCreate的第一行:

System.setProperty("http.keepAlive", "false");

What was happening is that without setting this property, your connection was kept alive waiting for more requests to be done, and was only returning when it timed out. 发生的事情是,没有设置此属性,您的连接将保持活动状态,等待更多请求完成,并且仅在超时时返回。 Setting it to false makes it return right when your request has completed. 将其设置为false可以使其在请求完成后立即返回。 To understand this concept better, read this answer: How to interpret "Connection: keep-alive, close"? 为了更好地理解该概念,请阅读以下答案: 如何解释“连接:保持活动,关闭”? .

Regarding the inefficient stream reading, I would recommend you to change your implementation to something similar to the following code: 关于流读取效率低下,我建议您将实现更改为类似于以下代码的内容:

URL url = new URL(params[0]);
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
StringBuilder builder = new StringBuilder();
String current;
while ((current = reader.readLine()) != null) {
    builder.append(current);
}
line = builder.toString();
reader.close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM