-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
TransmittableThreadLocal.Transmitter#restore 方法是否会导致内存泄漏? #321
Comments
简单的回答: 内存泄漏问题 在具体场景时可以构造对应的Case有效地测试到; 当然测试 不能替代 基于代码逻辑的分析。 基于的代码分析你继续完成,以形成结论? @StrongBanana 💕 🙏 针对你这个问题,分析代码 可能是一个最可信的方式。 如果分析过程有困难,可以看看、参考 下面的内容: @StrongBanana
|
下面简单地说明一下 恢复过程涉及的上下文
对于任意一个在
|
不会。 @nicky1
|
@oldratlee 你好,在本地测试过程中,遇到一个问题,为了要清理调用线程的ThreadLocal,所以在拦截器中的afterCompletion()中做了清理。 类似的示例代码,使用spring 拦截器: public class UserInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String userId = request.getParameter("userId");
if (StringUtils.isNotBlank(userId)) {
UserInfo userInfo = new UserInfo();
userInfo.setUserId(Integer.valueOf(userId));
ThreadContext.bindUser(userInfo);
}
return true;
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
ThreadContext.unBindUser();
super.afterCompletion(request, response, handler, ex);
}
} 业务入口代码:会发现 偶尔取到的userId值是0 @GetMapping("/test/ttl/pool")
public ResponseEntity ttlPool(@RequestParam Integer userId) {
CompletableFuture.runAsync(() -> {
// 模拟业务耗时
try {
TimeUnit.MILLISECONDS.sleep(200);
} catch (InterruptedException e) {
}
UserInfo user = ThreadContext.getUser();
Integer contextValue = Objects.isNull(user) ? 0 : user.getUserId();
log.info("传入的userId和从线程上下文获取的是否一致,flag:{},传入userId:{},上下文获取:{}", (userId.equals(contextValue)), userId, contextValue);
}, commonThreadPool);
return ResponseEntity.ok().build();
} 猜测是 因为拦截器中先执行了清理,等到线程异步执行时,主线程中的threadlocal 已经被清理掉了。 |
关于 因为这些与 |
restore
方法最终会调用到ThreadLocal.set()
,之后没有主动调用remove
;是否会因此产生内存泄漏?The text was updated successfully, but these errors were encountered: