apache / apisix

The Cloud-Native API Gateway
https://apisix.apache.org/blog/
Apache License 2.0
13.97k stars 2.45k forks source link

bug: Memory leak when logger log the traffic with large (about 128K) response body #11244

Open caogw279 opened 1 month ago

caogw279 commented 1 month ago

Current Behavior

When configured kafka-logger in global rules with include_resp_body set to true and upstream server response 128K body, the memory will increase during the test and never get down, if the test continue, the memory usage will continue increase until the OOM happen.

Expected Behavior

The memory usage will keep in a range during the test and will return to normal after the stress stop.

Error Logs

No error logs, please have look at output of "free" and the RES of very worker process

apisix_memory_leak

Steps to Reproduce

  1. Run apisix in CentOS
  2. APISIX Config route with Admin API
  3. APISIX Config kafka-logger to globale_rules with include_resp_body set to true
  4. Stress the APISIX (with Avalanche, server response 128K )
  5. Monitor the system memory usage (such as via order free -h)
  6. Check the memory usage of every worker process (such as via top)

Environment

caogw279 commented 1 month ago

Addition: I test it with wrk as client and take the nginx as the upstream server, and change it's index.html with 512k bytes characters, can't reproduce the issue, resident memory of every openresty process will keep at 1 - 1.5 G no matter how long wrk stress it.

caogw279 commented 1 month ago

From the document [gc public API] (https://apisix.apache.org/docs/apisix/control-api/) I found a method which will trigger a full garbage collection in the HTTP subsystem, it's works, the memory not leak ? just hold by GC ? but why doesn't GC collection work by itself, why the memory is exhausted and GC still not work ?