Memory leak happening with Python when creating numpy diagonal array and doing matrix math

3 weeks ago 17
ARTICLE AD BOX

P = y.T @ (np.diag(Wc) @ y) This line is creating increased memory usage for me in the below code on my Windows 11 machine. As soon as I run the code, I see the RAM usage increasing and skyrocketing. But when I replace this line with P = y.T @ (y * Wc[:, None]) , the RAM usage is stable. However, I tested the same code on my Ubuntu 20.04 pc and it worked fine for both the alterations. I am not able to understand the strange behavior. Experts, please help.

Ubuntu 20.04; Python 3.10.12; Numy: 1.26.4

I can update the post with Windows PC Python/ numpy versions once I access it again.

import numpy as np, psutil, os, time, gc p = psutil.Process(os.getpid()) def rss_mb(): return p.memory_info().rss / 1024 / 1024 n = 3 kmax = 2*n + 1 Wc = np.random.randn(kmax) y = np.random.randn(kmax, n) print("start rss", rss_mb()) i = 0 while True: P = y.T @ (np.diag(Wc) @ y) # <- same structure as FilterPy # P = y.T @ (y * Wc[:, None]) #modified i += 1 if i % 500 == 0: gc.collect() print(i, "rss", rss_mb()) time.sleep(0.1)
Read Entire Article