[ci skip] Make test more robust
Fix GH-14637: memory leak after failed heap allocation due to mem limit. (#14641)