当前位置:网站首页>Dirty data and memory leakage of ThreadLocal
Dirty data and memory leakage of ThreadLocal
2022-07-25 08:13:00 【lzhfdxhxm】
background :
Spring-Boot 2.0.8.RELEASE
Spring-Cloud 2.0.4.RELEASE
tomcat 8.5.37
JDK 1.8
Project use ThreadLocal To pass parameters , such as : Request data 、 Set a variable
ThreadLocalParamsUtilpublic class ThreadLocalParamsUtil {
private static final Log log = LogFactory.getLog(ThreadLocalParamsUtil.class);
private static final ThreadLocal<Map<String,Object>> threadLocalMap=new ThreadLocal<>();
public static <T>T get(String key){
// ...
}
public static void set(String key, Object value) {
}
public static void clean(String key) {
}
public static void cleanAll() {
}When the interface returns normally , call ThreadLocalParamsUtil.cleanAll() Cleared all shared variables .
When an exception occurs in the interface processing process , and No, call ThreadLocalParamsUtil.cleanAll() Clear all shared variables .
problem :
In the process of interface processing ,ThreadLocalParamsUtil.get("myKey"), occasionally The value obtained is Dirty data , Cause program processing exception .
Simulation scenario :( The actual occurrence scenario is actually quite complex , It's simplified here )
When an exception occurs in the interface processing , Because shared variables are not cleared , The shared variables in this thread always exist , Memory leaks and dirty data have occurred .
When subsequent requests use This thread with exceptions When ,ThreadLocalParamsUtil.get("myKey") You will get the value set when the thread last requested , That's dirty data .
Why not get dirty data every time ? because tomcat When processing requests, you use Thread pool , The default number of core threads is 10, Then it may pass 10 After requests , You will get dirty data once , Cause program processing exception .
Use matters needing attention :
ThreadLocal Shared variables within threads , Convenience is convenience , But pay attention every time set after , You don't need to use this key When , must do remove Clean up , Otherwise, it will not only cause memory leakage , It may also lead to dirty data , So there are some strange mistakes .
notes :springboot built-in tomcat Thread pool of :( Reference resources :https://blog.csdn.net/qq_31086797/article/details/110523079)
Parameters
Number of core threads in thread pool
server.tomcat.min-spare-threads: The parameter is tomcat Number and size of core threads processing business , The default value is 10
Maximum number of threads in the thread pool
server.tomcat.max-threads: The parameter is tomcat The maximum number and size of threads processing business , The default value is 200, When you have a value for concurrency, you can increase this parameter
Maximum connections requested
server.tomcat.max-connections: This parameter is the maximum number of connections requested , The default value is 10000, Note that this parameter is not set on the thread pool , But in tomcat Of Acceptor class ( Thread class that deals specifically with connections ) Controlled
边栏推荐
- 刷题《剑指Offer》day02
- Introduction to machine learning (I): understanding maximum likelihood estimation in supervised learning
- Uiautomator2 common commands
- Test the mock data method of knowing and knowing
- Message Oriented Middleware
- 【黑马程序员】Redis学习笔记004:主从复制+哨兵模式+集群
- 第3章业务功能开发(实现全选按钮实时的响应)
- Summary of SQL skills in data warehouse development
- "Unable to recognize" yarn "item as cmdlet, function, script file
- 475-82(230、43、78、79、213、198、1143)
猜你喜欢

Introduction to machine learning (I): understanding maximum likelihood estimation in supervised learning
Technical Analysis | Doris connector combined with Flink CDC to achieve accurate access to MySQL database and table exactly once

一款强大的端口扫描工具(nmap)

Redis学习

VS2019 C# MFC安装

MVC mode three-tier architecture

【黑马程序员】Redis学习笔记001:Redis简介+五种基本数据类型

Refreshing mobile terminal personal center page

Advanced C language (XIII) - Example Analysis of dynamic memory management

刷题《剑指Offer》day02
随机推荐
Redis core principles
Advanced C language (XII) - dynamic memory management
Changes were made to tables that cannot be recreated or the prevent saving changes that require the table to be recreated option was enabled
Raspberry pie uses the command line to configure WiFi connections
Chapter 3 business function development (query clues)
【keras bug】Tensor is unhashable if Tensor equality is enabled. Instead, use tensor.experimental_ ref(
Some easy-to-use plug-ins and settings installed in vscode
【黑马程序员】Redis学习笔记001:Redis简介+五种基本数据类型
Can Flink's current capabilities support the synchronization of a table from source (MySQL) to sink (MySQL) to separate databases and tables
Test the mock data method of knowing and knowing
Interview questions: common faults and occurrence scenarios of redis
People who lose weight should cry: it's no good not eating food, because your brain will be inflamed
文献学习(part101)--CONVEX BICLUSTERING
[5g NR] 3GPP common protocol sorting
C 43. Get UDP available ports
Redis学习
Network packet loss, network delay? This artifact helps you solve all problems
Raspberrypico analytic PWM
Implement hot post | community project with timed tasks and cache
[QNX Hypervisor 2.2用户手册]9.3 cpu