Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak when closing editor or console tabs #18764

Closed
10 tasks
impact27 opened this issue Jul 21, 2022 · 3 comments · Fixed by #18781
Closed
10 tasks

Memory leak when closing editor or console tabs #18764

impact27 opened this issue Jul 21, 2022 · 3 comments · Fixed by #18781

Comments

@impact27
Copy link
Contributor

Issue Report Checklist

  • Searched the issues page for similar reports
  • Read the relevant sections of the Spyder Troubleshooting Guide and followed its advice
  • Reproduced the issue after updating with conda update spyder (or pip, if not using Anaconda)
  • Could not reproduce inside jupyter qtconsole (if console-related)
  • Tried basic troubleshooting (if a bug/error)
    • Restarted Spyder
    • Reset preferences with spyder --reset
    • Reinstalled the latest version of Anaconda
    • Tried the other applicable steps from the Troubleshooting Guide
  • Completed the Problem Description, Steps to Reproduce and Version sections below

Problem Description

Every time a tab is closed, the code editor is not removed from memory.
The full list of added object can be seen here: (Executed in the internal console)

>>> objgraph.show_growth(limit=1000)
tuple                         37021      +120
dict                          68206       +58
QShortcut                       280       +42
Shortcut                        254       +42
list                          74018       +38
method                         1774       +32
cell                          12439       +26
QColor                          247       +26
function                      63060       +24
SpyderAction                    559       +23
weakref                       12590       +21
QTimer                          105       +16
QTextBlock                      498       +10
QTextCharFormat                  86        +9
QBrush                           28        +7
QPen                             28        +7
QAction                         162        +5
set                            4836        +3
QMutex                           15        +3
FoldingRegion                   344        +3
partial                         694        +2
QMenu                            22        +2
QComboBox                        10        +2
QTextCursor                      12        +2
QScrollBar                       12        +2
IntervalTree                      9        +2
QStaticText                       8        +2
SortedDict                        9        +2
SortedList                        9        +2
builtin_function_or_method     5421        +1
Signals                          35        +1
Context                          37        +1
diff_match_patch                  6        +1
Property                         88        +1
QThread                          13        +1
QWidget                          89        +1
QHBoxLayout                     103        +1
QFont                            24        +1
LineNumberArea                    6        +1
PythonSH                          6        +1
QObject                           6        +1
QTextDocument                     7        +1
QPoint                            6        +1
CallTipWidget                     7        +1
QBasicTimer                       7        +1
ToolTipWidget                     5        +1
TextDecorationsManager            5        +1
CompletionWidget                  7        +1
HTMLDelegate                      5        +1
Future                           13        +1
Handle                           20        +1
QtKillRing                        5        +1
KillRing                          5        +1
CodeEditor                        4        +1
TextHelper                        4        +1
PanelsManager                     4        +1
EdgeLine                          4        +1
IndentationGuide                  4        +1
FoldingPanel                      4        +1
DelayJobRunner                    4        +1
DebuggerManager                   4        +1
DebuggerPanel                     4        +1
ClassFunctionDropdown             4        +1
ScrollFlagArea                    4        +1
DocstringWriterExtension          4        +1
QEventLoop                        4        +1
EditorExtensionsManager           4        +1
SnippetsExtension                 4        +1
CloseBracketsExtension            4        +1
Index                             4        +1
IndexHandle                       4        +1
OutlineExplorerProxyEditor        4        +1
patch_obj                         3        +1
CloseQuotesExtension              4        +1
TextDecoration                    4        +1
PropertyHandle                    4        +1
Node                            357        +1
FoldingStatus                     4        +1
Interval                        496        +1
PythonWorker                      3        +1

A lot of these objects, including LineNumberArea and Shortcut, are linked with CodeEditor. The question is therefore why the code editor is never removed. One can inspect the reference graph with objgraph:
completion_widget
The problem is therefore that the parent of each new CompletionWidget is set to the main window and never removed.
It makes more sense to have the CodeEditor as a parent, but that only displaces the problem, as the new shortest chain is:
completion_plugin
CompletionPlugin.requests grows indefinitely.

What steps reproduce the problem?

What is the expected output? What do you see instead?

Paste Traceback/Error Below (if applicable)

PASTE TRACEBACK HERE

Versions

  • Spyder version:
  • Python version:
  • Qt version:
  • PyQt version:
  • Operating System name/version:

Dependencies

PASTE DEPENDENCIES HERE
@impact27
Copy link
Contributor Author

impact27 commented Jul 21, 2022

I digged a bit more and found two other sources of CodeEditor leaks:
This one is the expected graph, but the CodeEditor is never removed. This can be solved by calling editor.setParent(None) in EditorStack.close_file
CodeEditor2
The other one shows that the shortcut list (_shortcut_data) is never emptied:
CodeEditor4

@ccordoba12
Copy link
Member

ccordoba12 commented Jul 21, 2022

Hey @impact27, thanks a lot for taking a look at these issues! About what you said:

The other one shows that the shortcut list (_shortcut_data) is never emptied:

Is it not be possible to clear that on EditorStack.close_file as well?

@impact27
Copy link
Contributor Author

impact27 commented Jul 21, 2022

  • Set CompletionWidget parent to None
  • Set CodeEditor parent to None
  • Use weakref in requests
  • unregister the shortcuts

@ccordoba12 ccordoba12 changed the title Memory leak when closing a tab Memory leak when closing editor or console tabs Jul 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants