在Winform使用RDLC時,在批量打印情況下,內存隨着打印任務的數量逐漸增加。即便手動GC效果也不明顯。
原因:
localReport在創建時,每個實例都是一個應用程序域。租約的過期時間比較久,按照網上的資料,過期時間大約10分鐘左右。
這就導致即便打印完了,內存也不會很快下降。
解決辦法:
1. 手動釋放RDLC資源
report.ReleaseSandboxAppDomain();
report.Dispose();
2. 修改系統裏的租約相關的時間
網上給到的代碼是
LifetimeServices.LeaseTime = TimeSpan.FromSeconds(5);//租約 LifetimeServices.LeaseManagerPollTime = TimeSpan.FromSeconds(5);//檢查間隔 LifetimeServices.RenewOnCallTime = TimeSpan.FromSeconds(1);//續訂時間 LifetimeServices.SponsorshipTimeout = TimeSpan.FromSeconds(5);//等待響應時間
措施1和2同時使用時,連續打印100個RDLC,內存在70-85浮動。比之前的每個報表增加1MB內存好很多。
遺留問題
打印RDLC(導出PDF)會產生臨時文件。大量打印時,臨時文件夾會很大。如何避免?