gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 23:28:53 25 4
gpt4 key购买 nike

我正在使用 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();
}

}

TIA,
维杰

最佳答案

据我所知,是的,这是关闭 Jersey 客户的正确方法 ...带有以下警告。

1)您要防止的不是内存泄漏,而是连接(到您正在处理的服务器)泄漏......

2) 以下是关于Client类(class) Chapter 3 of the Jersey Handbook :

Client instances are expensive resources. It is recommended a configured instance is reused for the creation of Web resources. The creation of Web resources, the building of requests and receiving of responses are guaranteed to be thread safe. Thus a Client instance and WebResource instances may be shared between multiple threads



因此,如果您打算调用多个电话, 是个好主意不是 每次调用都调用destroy。 WebResources 也是如此(但程度较轻)。

3)我所描述的是来自 Jersey 1.1(但我看到了 threads about this as far back as 2009 )。

关于memory-leaks - Jersey 客户端连接关闭内存泄漏问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8012680/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com