ThreadLocal内存泄漏问题

发生场景
在spring boot项目中我在postHandle(登录拦截)中,通过ThreadLocal判断其中有没有存储user信息来判断是否将user信息给到前端。
但是在我退出登录之后(通过setUser(null))来清除存储的user信息之后。
连续请求首页,请求首页时调用了我的ThreadLocal.getUser(),方法在连续请求10几次之后,突然能请求到一次用户信息。

2022-05-02 22:51:30.845  INFO 26368 --- [nio-8001-exec-7] com.hwx.goodsSystem.Component.goodsPand  : session:        null
2022-05-02 22:51:30.845  INFO 26368 --- [nio-8001-exec-7] com.hwx.goodsSystem.Component.goodsPand  : user:   user(id=3, userName=10086@qq.com, userPassword=null, sex=null, age=null, salt=null, petName=null, imageUrl=/img/hwx.png, dwell=null, signature=null, createTime=Sat Apr 30 22:52:52 CST 2022, updateTime=Sat Apr 30 22:52:52 CST 2022)
2022-05-02 22:51:31.014  INFO 26368 --- [nio-8001-exec-9] com.hwx.goodsSystem.Component.goodsPand  : session:        null
2022-05-02 22:51:31.014  INFO 26368 --- [nio-8001-exec-9] com.hwx.goodsSystem.Component.goodsPand  : user:   null
2022-05-02 22:51:31.168  INFO 26368 --- [nio-8001-exec-8] com.hwx.goodsSystem.Component.goodsPand  : session:        null
2022-05-02 22:51:31.168  INFO 26368 --- [nio-8001-exec-8] com.hwx.goodsSystem.Component.goodsPand  : user:   null
2022-05-02 22:51:31.353  INFO 26368 --- [nio-8001-exec-1] com.hwx.goodsSystem.Component.goodsPand  : session:        null
2022-05-02 22:51:31.354  INFO 26368 --- [nio-8001-exec-1] com.hwx.goodsSystem.Component.goodsPand  : user:   null
2022-05-02 22:51:31.525  INFO 26368 --- [nio-8001-exec-4] com.hwx.goodsSystem.Component.goodsPand  : session:        null
2022-05-02 22:51:31.526  INFO 26368 --- [nio-8001-exec-4] com.hwx.goodsSystem.Component.goodsPand  : user:   null
2022-05-02 22:51:31.684  INFO 26368 --- [nio-8001-exec-3] com.hwx.goodsSystem.Component.goodsPand  : session:        null
2022-05-02 22:51:31.684  INFO 26368 --- [nio-8001-exec-3] com.hwx.goodsSystem.Component.goodsPand  : user:   null
2022-05-02 22:51:31.849  INFO 26368 --- [nio-8001-exec-2] com.hwx.goodsSystem.Component.goodsPand  : session:        null
2022-05-02 22:51:31.850  INFO 26368 --- [nio-8001-exec-2] com.hwx.goodsSystem.Component.goodsPand  : user:   user(id=3, userName=10086@qq.com, userPassword=null, sex=null, age=null, salt=null, petName=null, imageUrl=/img/hwx.png, dwell=null, signature=null, createTime=Sat Apr 30 22:52:52 CST 2022, updateTime=Sat Apr 30 22:52:52 CST 2022)
2022-05-02 22:51:32.008  INFO 26368 --- [io-8001-exec-10] com.hwx.goodsSystem.Component.goodsPand  : session:        null
2022-05-02 22:51:32.009  INFO 26368 --- [io-8001-exec-10] com.hwx.goodsSystem.Component.goodsPand  : user:   null
2022-05-02 22:51:32.187  INFO 26368 --- [nio-8001-exec-7] com.hwx.goodsSystem.Component.goodsPand  : session:        null
2022-05-02 22:51:32.188  INFO 26368 --- [nio-8001-exec-7] com.hwx.goodsSystem.Component.goodsPand  : user:   user(id=3, userName=10086@qq.com, userPassword=null, sex=null, age=null, salt=null, petName=null, imageUrl=/img/hwx.png, dwell=null, signature=null, createTime=Sat Apr 30 22:52:52 CST 2022, updateTime=Sat Apr 30 22:52:52 CST 2022)
2022-05-02 22:51:32.358  INFO 26368 --- [nio-8001-exec-9] com.hwx.goodsSystem.Component.goodsPand  : session:        null
2022-05-02 22:51:32.359  INFO 26368 --- [nio-8001-exec-9] com.hwx.goodsSystem.Component.goodsPand  : user:   null

具体原因
所以及时调用ThreadUser.remove();方法清除

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爷可是个天才

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值