Skip to content

Memory leak from using tf.constant in loop in TF 2.16 #68196

Closed

Description

@declanoller

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:

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
...

Activity

  1. SuryanarayanaY commented on May 22, 2024

    @SuryanarayanaY
    Collaborator

    Hi @declanoller ,

    I have tested the code in tf-nightly (dev20240520) and found no memory leak. Please refer to attached gist.

  2. added
    stat:awaiting response
    type:performance
    on May 22, 2024
  3. SuryanarayanaY commented on May 22, 2024

    @SuryanarayanaY
    Collaborator

    Tested with Tf2.16 also and found no memory leak.Gist for reference.

  4. removed
    stat:awaiting response
    on May 22, 2024
  5. declanoller commented on May 22, 2024

    @declanoller
    Author

    @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:

    2.16.1
    3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0]
    

    Can you test it with 3.12 somehow?

  6. added
    comp:ops
    stat:awaiting tensorflower
    and removed on Jun 5, 2024
  7. hzhongresearch commented on Jul 18, 2024

    @hzhongresearch

    I have also encountered this bug. It can be triggered using the following code.

    import tensorflow as tf
    while True:
        dummyData = tf.zeros(shape=(160000,1),dtype=tf.int16)
    

    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.

    TensorFlow OS Python Bug
    2.17.0 Debian 12 3.11.2 No
    2.17.0 Debian 11 3.9.2 No
    2.17.0 Ubuntu 24.04 3.12.3 No
    2.16.2 Debian 12 3.11.2 Yes
    2.16.2 Debian 11 3.9.2 No
    2.16.2 Ubuntu 24.04 3.12.3 Yes
    2.16.1 Debian 12 3.11.2 Yes
    2.16.1 Debian 11 3.9.2 No
    2.16.1 Ubuntu 24.04 3.12.3 Yes
  8. 1 remaining item

  9. added a commit that references this issue on Feb 2, 2026
    167dbd5
  10. added 2 commits that reference this issue on Mar 18, 2026
    b832aa2
    eeabd29
  11. added
    awaiting PR merge
    and removed
    stat:awaiting tensorflower
    on Mar 31, 2026
  12. removed
    awaiting PR merge
    on Jul 22, 2026
  13. Kayyuri commented on Jul 23, 2026

    @Kayyuri

    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.

  14. added
    stat:awaiting response
    on Jul 23, 2026
  15. github-actions commented on Jul 31, 2026

    @github-actions

    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.

  16. added
    stale
    on Jul 31, 2026
  17. github-actions commented on Aug 7, 2026

    @github-actions

    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.

  18. google-ml-butler commented on Aug 7, 2026

    @google-ml-butler

    Are you satisfied with the resolution of your issue?
    Yes
    No

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

TF 2.16comp:opsstalestat:awaiting responsetype:performance

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions