[New-bugs-announce] [issue39782] local varible referenced a Exception won't be collected in function

Wang Jie report at bugs.python.org
Fri Feb 28 06:18:30 EST 2020

New submission from Wang Jie <i at i8e.net>:

I referenced an Exception object in a function and found memory usage will increase constantly in the accident. I think it may be a bug.

I wrote a minimal code to reproduce it.

from threading import local, Thread
from time import sleep

l = {}

def t0():
  b = l.get('e') # memory usage won't increase if I remove this line
    raise Exception('1')
  except Exception as e:
    l['e'] = e

def target():
  while True:


# t = Thread(target=target)
# t.daemon = True
# t.start()

I tried to execute it in IPython and got the following output:

In [1]: run py/ref_exception_causes_oom.py

In [2]: import objgraph

In [3]: objgraph.show_growth(limit=3)
frame        78792    +78792
Exception    78779    +78779
traceback    78779    +78779

In [4]: objgraph.show_growth(limit=3)
Exception   100862    +22083
traceback   100862    +22083
frame       100875    +22083

In [5]: objgraph.show_growth(limit=3)
Exception   115963    +15101
traceback   115963    +15101
frame       115976    +15101

And I tried to execute this code in python2.7 and pypy. Both of them won't occur this problem.

components: Interpreter Core
messages: 362873
nosy: wangjie
priority: normal
severity: normal
status: open
title: local varible referenced a Exception won't be collected in function
type: resource usage
versions: Python 3.7

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list