tensorflow / tfjs

A WebGL accelerated JavaScript library for training and deploying ML models.
https://js.tensorflow.org
Apache License 2.0
18.51k stars 1.94k forks source link

Possible memory leak when reloading a model after disposing of it. #8459

Open stevexbritton opened 4 days ago

stevexbritton commented 4 days ago

System information

Describe the current behavior After loading and using a LayersModel I call model.dispose() and tf.disposeVariables() to release the tf memory. However, if I reload the model to use it again memory is leaked, at least 16k of Array data. This occurs each time around the loop.

Describe the expected behavior I would not expect a memory leak and would expect it to behave the same as if the model was just reused.

Standalone code to reproduce the issue The url "https://vykingsneakerkitnative.s3.eu-central-1.amazonaws.com/SteveTest/tmp/tf-leak-test.html" demonstrates the problem. Steps to demonstrate:

  1. Load the page "https://vykingsneakerkitnative.s3.eu-central-1.amazonaws.com/SteveTest/tmp/tf-leak-test.html" in chrome and open Developer Tools
  2. Click "Run Test1" button.
  3. Garbage collect and take a memory sample.
  4. Click "Run Test1" button again.
  5. Garbage collect and take another memory sample.
  6. Comparing sample 2 with sample 1 shows the "Array" objects has increased by about 16K

Steps to demonstrate model reuse with minimal memory grown

  1. Load the page in chrome and open devtools (or do a page reload)
  2. Click "Run Test2" button.
  3. Garbage collect and take a memory sample.
  4. Click "Run Test2" button again.
  5. Garbage collect and take another memory sample.
  6. Comparing sample 2 with sample 1 shows the "Array" objects has increased only by about 96

Other info / logs Include any logs or source code that would be helpful to diagnose the problem. If including tracebacks, please include the full traceback. Large logs and files should be attached.