HttpClient的内存管理 [英] HttpClient memory management

查看:147
本文介绍了HttpClient的内存管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,有一个线程池(ThreadPoolExecutor的),那就是每一个执行HTTPGET操作和读入的InputStream一个byte []交给任务的应用程序来做些事情。

I have an application that has a thread pool (ThreadPoolExecutor) that is handed tasks that each perform a HttpGet operation and read the InputStream into a byte[] to do something with.

阅读HttpClient的文档后,我离开时与IM pression,最好的方式来管理的HttpClient在多个线程的连接是建立一个单一的ThreadSafeClientConnManager,并通过分享它的应用程序。

After reading the HttpClient docs I came away with the impression that the best way to manage HttpClient connections across multiple threads is to create a single ThreadSafeClientConnManager and share it through out the application.

实施此操作后,我注意到,所有的任务,即使完成仍有存储器仍然正在使用的ThreadSafeClientConnManager一个显著量

After implementing this, I am noticing that even after all of the tasks are completed there is still a significant amount of memory still being used by the ThreadSafeClientConnManager.

看着堆转储,这款内存是字节[]数组的形式。这些都不是正在举行的是我创造的任何引用。他们正在举行由ThreadSafeClientConnManager和游泳池件。我不知道,如果他们都涉及到InputStreams或者如果他们是另一回事。

Looking at the heap dump, this memory is in the form of byte[] arrays. These are not being held by any references that I created. They are being held by pieces of the ThreadSafeClientConnManager and its pool. I am not sure if they are related to the InputStreams or if they are something else.

所有的自己和自己的变量都成功地垃圾回收的任务。

All of the tasks themselves and their variables are successfully garbage collected.

如果我叫getConnectionManager()。关闭()在ThreadSafeClientConnManager那么所有的内存被释放就好了。不过,我不希望有关闭连接,因为这些HTTPGET任务可能发生在任何时间。我想申请生活的持续时间内保持打开状态。

If I call getConnectionManager().shutdown() on the ThreadSafeClientConnManager then all of the memory is freed just fine. However, I do not want to have to shutdown the connection, because these HttpGet tasks could happen at anytime. I would like to leave it open during the duration of the applications life.

由于HTTPGET任务运行,正在举行的内存的增长越来越多,最终导致内存不足的错误。当任务完成后,不会释放内存。

As the HttpGet tasks run, the memory being held grows more and more and can eventually lead to out of memory errors. When the tasks complete, the memory is not released.

我怎样才能确保内存的释放被使用它完成任务后?

How can I ensure the memory is released after the task that was using it is finished?

下面是code我使用。这是拼凑起来的是最好的,我在这里SO和在线code从HttpClient的文档,其他的问题。

Here is the code I am using. It is pieced together as best as I code from the HttpClient docs, other questions here on SO and online.

HttpClient的的创建:

The creation of the HttpClient:

// Create and initialize HTTP parameters
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 40 * 1000);
HttpConnectionParams.setSoTimeout(params, 40 * 1000);
ConnManagerParams.setMaxTotalConnections(params, 100);
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);

// Create and initialize scheme registry 
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register( new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));

// Create an HttpClient with the ThreadSafeClientConnManager.
// This connection manager must be used if more than one thread will
// be using the HttpClient.
ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
mHttpClient = new DefaultHttpClient(cm, params);

然后,它执行HTTPGET了Runnable是pretty的多正是基于从的例子HttpClient的范例的<一个href="http://hc.apache.org/httpcomponents-client-ga/httpclient/examples/org/apache/http/examples/client/ClientConnectionRelease.java">Manual连接释放。这里是什么样子的一个例子:

Then, the Runnable that performs the HttpGet is pretty much based exactly on the example from the HttpClient examples for Manual connection release. Here is an example of what it looks like:

HttpClient httpclient = getTheSharedThreadSafeClientConnManager(); // Would return the mHttpClient from above
    try {
        HttpGet httpget = new HttpGet("http://www.apache.org/");

        // Execute HTTP request
        System.out.println("executing request " + httpget.getURI());
        HttpResponse response = httpclient.execute(httpget);

        System.out.println("----------------------------------------");
        System.out.println(response.getStatusLine());
        System.out.println("----------------------------------------");

        // Get hold of the response entity
        HttpEntity entity = response.getEntity();

        // If the response does not enclose an entity, there is no need
        // to bother about connection release
        if (entity != null) {
            InputStream instream = entity.getContent();
            try {
                instream.read();
                // do something useful with the response
            } catch (IOException ex) {
                // In case of an IOException the connection will be released
                // back to the connection manager automatically
                throw ex;
            } catch (RuntimeException ex) {
                // In case of an unexpected exception you may want to abort
                // the HTTP request in order to shut down the underlying
                // connection immediately.
                httpget.abort();
                throw ex;
            } finally {
                // Closing the input stream will trigger connection release
                try { instream.close(); } catch (Exception ignore) {}
            }
        }

    }

有没有更多的,你必须做的每个任务释放资源?我在他们的ThreadSafeClientConnManager例子中看到他们用一个HttpContext的,但我不能找到如何使用它的任何文件。是必需的?如果是的话你怎么用的ThreadPoolExecutor使用它?

Is there more you have to do to release the resources per task? I saw in their ThreadSafeClientConnManager example they used a HttpContext, but I can't find any documentation on how to use it. Is that required? If so how do you use it with a ThreadPoolExecutor?

太感谢了。

推荐答案

你曾经调用ClientConnectionManager的releaseConnection(...)或closeExpiredConnections()方法?

Do you ever invoke ClientConnectionManager's releaseConnection(...) or closeExpiredConnections() methods?

这篇关于HttpClient的内存管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆