Memory leak with Jupyter Notebooks and ANSI colors in Firefox
- Dominant language
- Jupyter Notebook
- Stars
- 13.3k
- Forks
- 5.8k
- Avg merge
- 6d 11h
- Merged PRs (30d)
- 7
Description
**Describe the bug**
I seems that under specific circumstances, using ANSI colors repeatedly causes a huge memory leak in Firefox. The reproducing example does not trigger any memory use increase in Chromium.
**To Reproduce**
Here is the code to reproduce the bug.
```python
import time
import sys
for i in range(1, 101):
for j in range(1, 1001):
sys.stdout.write(f'\r\x1b[35mEpoch: \x1b[36m{i}/100 \x1b[35mStep: \x1b[36m{j}/1000 \x1b[35m100.00% \x1b[35m|\x1b[35mETA: \x1b[32m0.00s \x1b[35mloss:\x1b[94m 2.295335\x1b[35m acc:\x1b[94m 15.625000\x1b[0m')
sys.stdout.flush()
time.sleep(0.005)
print()
```
For the first 20 iterations of the main loop (for i in range), memory usage is normal. At the 40th iteration, the memory usage of Firefox has increased for as much as 5GB. From there, it still increases until OOM.
Without the colors, the memory usage is normal. The example prints multiple lines. When printing a single line repeatedly, the bug is not triggered. My educated guess is that it has something to do with removing and adding nodes in the DOM.
**Expected behavior**
Obviously, a constant memory usage as in Chromium would be preferable.
**Screenshots**
Here is the screenshot when running the reproducing code:

**Desktop (please complete the following information):**
- OS: Ubuntu/Linux
- Browser: Firefox 83 and Chromium 87
Contributor guide
Research direction
Start by running the provided Python reproduction in a Jupyter Notebook under Firefox 83 and compare memory behavior with Chromium 87, with and without ANSI colors and with one versus multiple lines. Trace the notebook output rendering path to isolate the DOM-node behavior; done means repeated colored multi-line output no longer grows Firefox memory toward OOM.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- jupyter-notebook, python
- Domain
- frontend, performance
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100