Host memory leak after save_checkpoint with the megatron backend
System Info
On Qwen2 7B model, we found 20GB consistent host memory leak after calling save_checkpoint with the megatron on.
The result on 4x GB200 (should be obvious with 2 GPUs or more):
Memory Leak Confirmed - Results Summary
Job 1218771 on xxx (GB200, 4 GPUs) completed successfully. Here are the results:
RSS Memory Before/After save_checkpoint
| Rank | Before (MB) | After (MB) | RSS Delta (MB) | gc.get_objects (MB) |
|---|---|---|---|---|
| 0 | 19,435 | 37,155 | 17,719 | 3,772 |
| 1 | 2,016 | 22,572 | 20,556 | 3,761 |
| 2 | 2,546 | 23,102 | 20,556 | 3,761 |
| 3 | 1,989 | 22,527 | 20,538 | 3,761 |
Key Findings
-
Massive host memory leak: ~17-20 GB of RSS leaked per rank after
save_checkpoint. For a 7B model with TP=4 (~1.9B params/rank), this is ~10x the expected model size per rank. -
gc.get_objects only accounts for ~3.8 GB — meaning most of the leaked memory (~16-17 GB) is not tracked by Python's garbage collector. This points to leaked memory in C extensions (likely torch tensors or megatron-core internal buffers that lost their Python references but weren't freed).
-
GPU memory is stable —
gpu_allocated_gbstays at ~17.7 GB before and after, so the leak is purely on the host side. -
Rank 0 started higher (19.4 GB vs ~2 GB for others) — likely because rank 0 handled the HuggingFace model download/loading.
Information
- [ ] The official example scripts
- [x] My own modified scripts
Tasks
- [x] An officially supported task in the
examplesfolder (such as GLUE/SQuAD, ...) - [ ] My own task or dataset (give details below)
Reproduction
Run the script with 2+ gpus.
Expected behavior
The host memory size increases in GBs after save_checkpoint is finished and won't restore.
Thank you for reporting this memory leak issue. This is a known problem with the Megatron backend's checkpoint saving mechanism.
Potential causes:
-
CUDA graph capture caching: Megatron may cache CUDA graphs that are not properly released after checkpoint saving.
-
NCCL communicator leakage: The distributed communication groups created during checkpoint saving may not be fully cleaned up.
-
Python reference cycles: Some checkpoint-related objects may create reference cycles preventing garbage collection.
Debugging steps:
- Check memory growth pattern:
import gc
import torch
# After each save_checkpoint
print(f"GPU memory: {torch.cuda.memory_allocated()/1e9:.2f}GB")
gc.collect()
torch.cuda.empty_cache()
- Enable memory profiling:
export TORCH_DISTRIBUTED_DEBUG=DETAIL
- Try disabling async checkpoint (if enabled):
checkpoint.save_async=False
Related PRs:
- PR #5575 is refactoring the checkpoint manager and may address this issue
Could you provide:
- Your exact checkpoint saving config
- Memory growth rate (GB per checkpoint)
- Whether the leak occurs with
save_async=False?
This will help identify the specific component causing the leak.
Fixed in https://github.com/verl-project/verl/pull/5705
Does this mean the memory leaks and excessive peak memory usage have been resolved?
Yes, I haven't observed OOM or memory leakage after the fix #5705 . If you are not using the main branch, just add actor_rollout_ref.actor.megatron.dist_ckpt_optim_fully_reshardable=False to your script.