File monitoring, out of memory

Why did the file monitoring function run out of memory?

Busy with the project, now simply paste some code and analyze the reason.

There is a configuration file in the project, which has been monitored, and this is for data. The red line part is the file modification

This is the method of file monitoring. The file modification calls the initialization method, and the bottom part of the initialization calls the above method.

This is the initialization method, and the above file monitoring method is called below. Looped. And the initialization method is synchronous. The above monitoring is 3s

The process of finding the problem is also relatively difficult, and I started to find out and start the project. After half an hour or so, the computer cpu is stuck at 100%

in conclusion. A thread is created in this place file listener. File modification calls a file listener again. The loop creates multiple threads. And the threads created later are all blocked. There are more and more threads. Out of memory.

How to determine what the problem is. Project started, start arthas. Call thread -n 10 to check thread information all the time. Found that the number of threads is high. See the code location in the thread stack information, and find that there is a method that has been calling the configuration file, and it is modified once in about 1s.

Solution: How to solve this problem. How should the code be modified?

Guess you like

Origin blog.csdn.net/haoweng4800/article/details/129153536