We have an interface with external interaction is adopted Netty HTTP, specific version netty-4.1.18, why use this version, I do not know, historical reasons.
Since Netty are asynchronous requests, there is always a unique business identity with external interactions that needs to be saved so that data can be blended before and after.
It is explained here that the bytebuf in the Netty need to release releases on their own when reading the channelread without writing the write operation. This is not related to this error, continue to say the key.
Check the log, first found the Outofdirectmemoryerror error, this error is also intermittent appearance, is obviously not enough memory, whether it is heap or direct.
Directly dump a copy of the heap memory log, the specific command: Jmap-dump:format=b,file=tomcat.dump PID. Our service is put on Tomcat, JPS, that Bootstrap is.
After dump, find the tool to see, recommend the use of Java JVISUALVM, in the Java directory of the bin, Java comes with the command is very useful, such as the above mentioned jps,jps-v than Ps-ef |grep XXX more convenient.
After loading the dump file:
Probably a glance, some instances of the class is also more, the whole of the Concurrenthashmap, Cliff has a problem, so make memory enough to blame, and then see the following netty about the class instance,
See Code use Attributekey Place, and see the source implementation, probably some of the things, should be the object created too much, resulting in memory overflow.
Each instance hangs this one concurrenthashmap, does not know whether our usage has the question, first regardless, first solves the problem,
Discovery code Each request creates a connection channel, each time the global save a Attributekey, in order to request an asynchronous response, blending raw data, so, millions of requests a day a little Chunkun autumn,
Borrow Attributekey's idea, instead of the global save a concurrenthashmap, then the key value pair to save the unique business identity, and after the asynchronous processing remove obsolete data.
Attributekey I turned over the source, I really did not see where to release the place, there is a set (null) of course the code is not used after the empty operation, but the Attributekey internal Haspmap is not automatically released.
After modification, redeploy. Observed for several days, dump a new memory log, and did not find too many object instances. Keep watching for a while.
In fact, the problem of memory overflow is not so difficult, memory is not enough to naturally fullgc or memory overflow, some memory leaks will eventually cause FULLGC or memory overflow, find too many garbage objects,
The release of the release, the singleton use of a single case, can be reused as far as possible reuse, should be no problem, look at the dump file basically found the root cause of the problem.
Pits Netty Io.netty.util.internal.OutOfDirectMemoryError