pytorch / pytorch/pytorch

Memory management, memory not returned after CPU -> GPU

Open
#177,520 0 comments 0 reactions 0 assignees View on GitHub
bot-triaged module: cpp module: cuda module: memory usage module: windows triaged
Dominant language
Python
Stars
103k
Forks
29.5k
PR merge metrics
PR metrics pending

Description

I have a simple C++ libtorch (2.10.0) program:

```
void PrintMemory(const char* label)
{
PROCESS_MEMORY_COUNTERS_EX info{};
GetProcessMemoryInfo(GetCurrentProcess(), reinterpret_cast(&info), sizeof(info));
printf("PrivateUsage (committed pages): %.3f GB\n", (float)info.PrivateUsage / 1024 / 1024 / 1024);
}
```

```
PrintMemory("Before");
auto tensor = torch::zeros({ 16384, 16384, 4 }); // also 4GB float32
PrintMemory("After Init");
tensor = tensor.to(torch::kCUDA);
PrintMemory("After Cuda");
c10::cuda::CUDACachingAllocator::emptyCache();
PrintMemory("After emptyCache");
```

```

[Before]
PrivateUsage (committed pages): 0.700 GB
[After Init]
PrivateUsage (committed pages): 4.716 GB
[After Cuda]
PrivateUsage (committed pages): 4.952 GB
[After emptyCache]
PrivateUsage (committed pages): 4.952 GB
```

But CPU memory is nor returned.

However, when I do this:

```
PrintMemory("Before");
auto tensor = torch::zeros({ 16384, 16384, 4 }); // also 4GB float32
PrintMemory("After Init");
tensor.reset();
PrintMemory("After reset");
```
```

[Before]
PrivateUsage (committed pages): 0.701 GB
[After Init]
PrivateUsage (committed pages): 4.716 GB
[After Reset]
PrivateUsage (committed pages): 0.701 GB
```

memory is returned.

Why in case of CUDA transfer, memory stays allocated (or seems to be allocated)? When I use the to CUDA transfer for large models (not a single Tensor like in this case), the RAM stays filled (or seems) and I cannot allocate more. GPU is also correctly allocated.

cc @peterjc123 @mszhanyi @skyline75489 @nbcsm @iremyux @Blackhex @jbschlosser @ptrblck @msaroufim @eqy @jerryzh168 @tinglvv @nWEIdia

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.