Yohan P.
Yohan P.

Reputation: 1

Why memory isn't liberate when the function is finished?

When I execute the program below, memory increases very quickly, so I suppose that memory used in the function named "secundary_function" isn't liberate. If I copy the element I append to the list the problem or if I don't use secundary_function the problem disappears. I'd like to understand why the copy is necessary here and why secundary_function has an influence on the memory used..

import numpy as np
import time

def main_function(N):
    liste_images = []

    for i in range(N) :
        images = np.zeros((3000,25,25))
        time.sleep(0.05)
        secundary_function(images)
        liste_images.append(images[0])

def secundary_function(images):
    conservee = np.arange(len(images))
    images[conservee]

main_function(6000)

Thank you for your answers and sorry for my english !

Upvotes: 0

Views: 67

Answers (1)

Tomasz Malesiński
Tomasz Malesiński

Reputation: 46

In this line:

liste_images.append(images[0])

images[0] creates a view of the 3000x25x25 images array. It means that the result of images[0] that you append to liste_images has a reference to the entire 3000x25x25 array. This big array will not be garbage collected. When you do a copy, you create a new 25x25 array and the big array can be freed in the next iteration of the for loop.

Upvotes: 2

Related Questions