Python Memory Management

Last day,I tried to feed some test images to my trained model to test the performance of my model, But I have faced some low-level problem.

I use this code to read image and send it to the detection function:

im = cv2.imread(im_file)

Everything is ok when I test my model with a little of images. But if I test my model with more than 1000 images, the test program would crash. After that, I ran the test program again and watched the memory usage. I noticed that the memory usage became full after a few iteration. So I need to do something to collect the useless memory.

The solution is delete the image object when it is useless.

del im

But the memory of the useless image object will not be collected immediately unless I call the collect() function of gc module.


If anything wrong, PLEASE tell me by e-mail or leave a message on this page.