Home > Back-end >  About okhttp3 under high concurrency problem that produced a great number of threads
About okhttp3 under high concurrency problem that produced a great number of threads

Time:03-24

I made A forwarding network system based on Java, demand is that the system is in the internal network (LAN), A system is in the outside network (Internet) B now A system needs to call B system interface, due to the security problem, the network can only be open to each other, but not directly use the Internet agent, this will cause A lot of risks,
So, can only develop A front system, A system of the request is sent to the front system, through the front system, to initiate the request, B system and the response results back to A system, the aim is to make the two systems can exchange network, and is limited to the URL configuration, not the whole network proxy, reduce risk,
Now the front system, I use is okHttp3 the network framework for a HTTP request, when doing performance testing, send, okHttp3 every request, will create a thread, thread in idle after 60 seconds, automatically recycling, problem here, under the high concurrency, add the number of threads is greater than the recovery of the number of threads, so the memory leak caused by not enough resources,
Saw the okhttp framework source code, found that it has a ConnectionPool class, there is a static initialization code thread pool,
The static {
Executor=new ThreadPoolExecutor (0, 2147483647, 60 l, TimeUnit. SECONDS, new SynchronousQueue will (), Util. ThreadFactory (" OkHttp ConnectionPool ", true));
}
From here you can see, it is 2147483647 maximum number of threads, free time is 60 seconds recycling, originally, the framework of the android system using object, because it is impossible for users to high concurrent to the point of a button, or there is no problem, won't produce how many threads, and a minute later will be recycled, but, I use in Java web system, forward requests high concurrency thousands of TPS, at this time is the memory leak problem, check the number of threads, as high as more than 2000,
I'm not sure whether I use has a problem, to request the great god of CSDN, what channels or whether to use parameter is set to solve the problem, thank you very much

CodePudding user response:

I also would like to know how to solve
  • Related