Jersey客户端连接关闭内存泄漏问题

Vij*_*jay 4 connection client memory-leaks jersey resource-leak

我正在使用Jersey v10并编写了以下代码.这是关闭Jersey客户端连接以避免内存泄漏的正确方法.我在此之前没有进行任何调用.

ClientConfig config = setupHttps();
    final Client c = Client.create(config);

    final WebResource r = c.resource(baseUri);
    ClientResponse response = null;
    try {
        response = r.path("/....")
                .header("contentId", id)
                .header("sid", sid).get(ClientResponse.class);
        ...



    } catch (Exception e) {
        log.error("Error returning contentServiceName.");

    } finally {
        if (response != null) {
            response.close();
        }
        if (c!= null) {
            c.destroy();
        }

    }
Run Code Online (Sandbox Code Playgroud)

TIA,Vijay

Eya*_*yal 8

据我所知,是的,这是关闭泽西岛客户的正确方法 ......有以下警告.

1)你想要防止的是内存泄漏,但连接(你要解决的服务器)泄漏......

2)以下内容是关于Jersey手册第3章中Client类:

客户端实例是昂贵的资源.建议重新使用已配置的实例来创建Web资源.Web资源的创建,请求的构建和响应的接收都保证是线程安全的.因此,可以在多个线程之间共享Client实例和WebResource实例

因此,如果您打算进行多次通话,最好不要为每次通话都调用destroy.WebResources也是如此(但程度较小).

3)我所描述的是来自泽西岛1.1(但我看到了早在2009年的线索).