3

Generally, I expect that when a local variable goes out of scope, it should be collected from the memory. Here, I experience something different. I tried to run the example given in scikit-fuzzy documentation (here) in a loop, but experience a memory leak. This is the minimal example,

from skfuzzy import control as ctrl
from pympler import muppy,tracker
import skfuzzy as fuzz
import numpy as np
import sys

def base_example():
    quality = ctrl.Antecedent(np.arange(0, 11, 1), 'quality')
    service = ctrl.Antecedent(np.arange(0, 11, 1), 'service')
    tip = ctrl.Consequent(np.arange(0, 26, 1), 'tip')
    quality.automf(3)
    service.automf(3)
    tip['low'] = fuzz.trimf(tip.universe, [0, 0, 13])
    tip['medium'] = fuzz.trimf(tip.universe, [0, 13, 25])
    tip['high'] = fuzz.trimf(tip.universe, [13, 25, 25])
    rule1 = ctrl.Rule(quality['poor'] | service['poor'], tip['low'])
    rule2 = ctrl.Rule(service['average'], tip['medium'])
    rule3 = ctrl.Rule(service['good'] | quality['good'], tip['high'])
    tipping_ctrl = ctrl.ControlSystem([rule1, rule2, rule3])
    tipping = ctrl.ControlSystemSimulation(tipping_ctrl)
    tipping.input['quality'] = 6.5
    tipping.input['service'] = 9.8
    tipping.compute()
if __name__ == '__main__':
    tr = tracker.SummaryTracker() # to show the initial list of objects
    for i in range(100000):
        base_example()
        if i%500==0: # we monitor different in the list of objects every 500 iterations
            print('Iteration ',i)
            tr.print_diff() # to show the difference beween this iteration and the base one

Since none of the objects in the base_example is stored in a global variable, I expect that when the function base_example reaches the end, the objects are deleted and the memory is freed, but doesnt happen here. Any ideas?

Matt
  • 41
  • 3
  • From this article it seems to suggest always ending your function with a return, even if you're not returning anything. https://stackoverflow.com/a/47958794/1491879 – Jacques MALAPRADE Jul 25 '22 at 17:08

0 Answers0