環境: PCF(AWS)
在production環境上傳幾張圖片后,app 內存暴增以及無法釋放導致crashed in a short time
解決方案:
- 解決內存不能主動釋放問題 - 項目文件增加了GC工作模式 相關文檔https://www.iaspnetcore.com/blog/blogpost/5a38af052d485225f4f40be9/net-core-two-kinds-of-gc-mode-server-gc-workstation-gc-settings
2. 解決內存暴增的問題 - 在管道中增加個GC回收的中間件
public class GCMiddleware { private readonly RequestDelegate _next; public GCMiddleware(RequestDelegate next) { _next = next; } public async Task Invoke(HttpContext httpContext) { await _next(httpContext); GC.Collect(2, GCCollectionMode.Forced, true); GC.WaitForPendingFinalizers(); } }
效果:
程序剛啟動
連續幾十次request之后:
等待幾分鍾后: