Repository navigation
Closed
Labels
TF 2.16comp:opsOPs related issuesstaleThis label marks the issue/pr stale - to be closed automatically if no activitystat:awaiting responseStatus - Awaiting response from authortype:performancePerformance Issue
Description
Issue type
Bug
Have you reproduced the bug with TensorFlow Nightly?
No
Source
binary from pip
TensorFlow version
2.16.1
Custom code
Yes
OS platform and distribution
Linux Ubuntu 24.04
Mobile device
No response
Python version
3.12.3
Bazel version
No response
GCC/compiler version
No response
CUDA/cuDNN version
No response
GPU model and memory
No response
Current behavior?
I believe I tracked down a memory leak in my code to a tf.constant creation in a short lived object of a class. I can reproduce it in an even simpler way by just creating the constant in a loop. No @tf.function decoration or model training necessary to cause it. It seems to happen if I replace the tf.constant with a tf.random.uniform(). I tried suggestions I've seen elsewhere, like trying to use del on the variable followed by gc.collect().
Some similar looking bug reports:
- Ram memory leak when using tf.function at tf 2.8,2.9 and 2.10, doesn't happen with tf 2.5 #57982
- Memory leak in custom training loop + tf.function #50765
- https://discuss.tensorflow.org/t/tensorflow-memory-leak-during-inference-in-loop/23044
It looks like upgrading to 2.15 solved it for some people, but it appears to be back in 2.16?
is this expected and I'm doing something wrong?
Standalone code to reproduce the issue
import time
import psutil
import tensorflow as tf
def loop_copy():
for _ in range(2000):
y = tf.constant(5.0) # still leaks with this
start_mem = psutil.Process().memory_info().rss
for _ in range(500):
time.sleep(0.1)
print((psutil.Process().memory_info().rss - start_mem) / 10**6)
loop_copy()
### Relevant log output
```shell
0.0
6.029312
6.815744
7.471104
8.126464
8.781824
9.437184
10.092544
10.747904
11.534336
12.189696
12.845056
13.500416
14.155776
14.811136
15.597568
16.252928
...
Reacted by Mehmet Fide
Metadata
Metadata
Assignees
Labels
TF 2.16comp:opsOPs related issuesstaleThis label marks the issue/pr stale - to be closed automatically if no activitystat:awaiting responseStatus - Awaiting response from authortype:performancePerformance Issue
Activity
SuryanarayanaY commentedon May 22, 2024
Hi @declanoller ,
I have tested the code in
tf-nightly(dev20240520) and found no memory leak. Please refer to attached gist.SuryanarayanaY commentedon May 22, 2024
Tested with Tf2.16 also and found no memory leak.Gist for reference.
declanoller commentedon May 22, 2024
@SuryanarayanaY what python version?
I have two machines. Both have TF 2.16.1. However, running the same code on the one with python 3.10 does not leak, while the machine (in the OP) with 3.12 does leak.
edit: I added a
print(sys.version)to your gist, it looks like it's using 3.10:Can you test it with 3.12 somehow?
hzhongresearch commentedon Jul 18, 2024
I have also encountered this bug. It can be triggered using the following code.
Once the code is run, memory usage can be monitored using a utility such as HTOP.
I have tested the code on the following x86/64 virtual machines on Google Cloud and found the following results.
1 remaining item
Add size limit to scalar TFE_TensorHandleCache to prevent memory leak
Add size limit to scalar TFE_TensorHandleCache to prevent memory leak
Add size limit to scalar TFE_TensorHandleCache to prevent memory leak
Kayyuri commentedon Jul 23, 2026
Hi @declanoller,
Thank you for opening this issue. Since this issue has been open for a long time, the code/debug information for this issue may not be relevant with the current state of the code base.
Could you try creating constants outside the loop?
The Tensorflow team is constantly improving the framework by fixing bugs and adding new features. We suggest you try the latest TensorFlow version with the latest compatible hardware configuration which could potentially resolve the issue. If you are still facing the issue, please create a new GitHub issue with your latest findings, with all the debugging information which could help us investigate.
Please follow the release notes to stay up to date with the latest developments which are happening in the Tensorflow space.
github-actions commentedon Jul 31, 2026
This issue is stale because it has been open for 7 days with no activity. It will be closed if no further activity occurs. Thank you.
github-actions commentedon Aug 7, 2026
This issue was closed because it has been inactive for 7 days since being marked as stale. Please reopen if you'd like to work on this further.
google-ml-butler commentedon Aug 7, 2026
Are you satisfied with the resolution of your issue?
Yes
No