"""
Based on example from https://pymotw.com/2/gc/index.html
Translated to Python3.
"""

import gc, sys
import pprint as pp

# Note: smaller threshold causes more frequent GC
try:
    threshold = int(sys.argv[1])
except (IndexError,  ValueError,  TypeError):
    threshold = 5
    print('>>>>> Missing param, using default of {0}'.format(threshold))

class MyObj(object):
    def __init__(self,  name):
        self.name = name
        print('>>>>> Created {0}'.format(self.name))

# only use for debugging
gc.set_debug(gc.DEBUG_STATS)

gc.set_threshold(threshold,  1,  1)
print('>>>>> Thresholds = ',  end='')
pp.pprint(gc.get_threshold())

print('>>>>> Clear collector by forcing run')
gc.collect()

print('>>>>> Creating objects')
objs = []
for i in range(4):
    objs.append(MyObj(i))
