Giter Site home page Giter Site logo

Bug: Memory leak about openadapt HOT 6 OPEN

openadaptai avatar openadaptai commented on July 22, 2024
Bug: Memory leak

from openadapt.

Comments (6)

abrichr avatar abrichr commented on July 22, 2024

First step: identify sources of memory leaks (there may be more than one!)

from openadapt.

abrichr avatar abrichr commented on July 22, 2024

https://docs.python.org/3/library/tracemalloc.html

from openadapt.

abrichr avatar abrichr commented on July 22, 2024

https://github.com/denis-ryzhkov/mem_top

from openadapt.

angelala3252 avatar angelala3252 commented on July 22, 2024

After using tracemalloc and pympler to profile a few longer recordings with a lot of ActionEvents, this is the output I get:

From tracemalloc, the top 3 largest remaining memory allocations after Ctrl + C:

C:\Users\Angel\Desktop\OpenAdapt.venv\lib\site-packages\mss\windows.py:287: size=9041 MiB, count=3430, average=2699 KiB
C:\Users\Angel\AppData\Local\Programs\Python\Python310\lib\multiprocessing\reduction.py:51: size=9122 KiB, count=140, average=65.2 KiB
<frozen importlib._bootstrap_external>:672: size=3018 KiB, count=6951, average=445 B

It's clear that the first one is the largest by a landslide, and following the traceback I found that it came from the function that returned the screenshots, _grab_impl in windows.py.

Using pympler, I printed out the newly created objects each time a key was pressed:

types |   # objects |   total size
================================ | =========== | ============
                       bytearray |         256 |      1.98 GB
                            dict |         729 |    169.21 KB
                  main.Event |         622 |     38.88 KB
                             int |        1241 |     33.95 KB
       mss.screenshot.ScreenShot |         256 |     18.00 KB
                           float |         626 |     14.67 KB
                  mss.models.Pos |         256 |     14.00 KB
                 mss.models.Size |         256 |     14.00 KB
               collections.deque |           0 |      4.12 KB
                            code |           0 |    386     B
                      memoryview |           2 |    368     B
              _winapi.Overlapped |           1 |    168     B
                            list |           2 |    144     B
         ctypes.c_wchar_Array_22 |           1 |    120     B
  pynput.keyboard._win32.KeyCode |           1 |     48     B

The bytearray was always the largest, which also points towards the screenshots being the issue.

Since the screenshots are put in the event queue in a loop, it seems like the most likely reason is that when a lot of input is given by the user, screenshots are put into the queue faster than they are removed from the queue, causing a pile-up of screenshots remaining in the queue.

from openadapt.

abrichr avatar abrichr commented on July 22, 2024

@KrishPatel13 @angelala3252 can you please confirm whether this fixed?

from openadapt.

abrichr avatar abrichr commented on July 22, 2024

Latest performance data:

2024-02-29 00:06:39.112 | INFO     | __main__:log_memory_usage:73 - source='File "/Users/abrichr/Library/Caches/pypoetry/virtualenvs/openadapt-VBXg4jpm-py3.10/lib/python3.10/site-packages/mss/darwin.py", line 238'
2024-02-29 00:06:39.113 | INFO     | __main__:log_memory_usage:74 -     new_KiB=4422056.185546875 total_KiB=4462521.392578125 new_blocks=177 total_blocks=179
2024-02-29 00:06:39.114 | INFO     | __main__:log_memory_usage:73 - source='File "/usr/local/Cellar/[email protected]/3.10.13_2/Frameworks/Python.framework/Versions/3.10/lib/python3.10/multiprocessing/reduction.py", line 51'
2024-02-29 00:06:39.114 | INFO     | __main__:log_memory_usage:74 -     new_KiB=26108.017578125 total_KiB=26108.4169921875 new_blocks=102 total_blocks=110
2024-02-29 00:06:39.114 | INFO     | __main__:log_memory_usage:73 - source='File "/Users/abrichr/Library/Caches/pypoetry/virtualenvs/openadapt-VBXg4jpm-py3.10/lib/python3.10/site-packages/mss/darwin.py", line 226'
2024-02-29 00:06:39.114 | INFO     | __main__:log_memory_usage:74 -     new_KiB=-23322.5556640625 total_KiB=0.0 new_blocks=-2 total_blocks=0
2024-02-29 00:06:39.187 | INFO     | __main__:wrapper_logging:129 -  <- Leave: process_events(None)
2024-02-29 00:06:48.405 | INFO     | __main__:log_memory_usage:77 - trace_str=
                      types |   # objects |   total size
=========================== | =========== | ============
                  bytearray |         176 |      4.18 GB
                _io.BytesIO |           4 |     25.49 MB
                      tuple |      169954 |     11.61 MB
                       dict |       17237 |      5.41 MB
                       list |       20874 |      3.12 MB
                        str |       34363 |      2.89 MB
                        int |       30835 |    843.79 KB
  tracemalloc.StatisticDiff |        2129 |    149.70 KB
      tracemalloc.Traceback |        2129 |     99.80 KB
          collections.deque |         157 |     96.70 KB
                       type |          53 |     67.10 KB
                       code |         192 |     49.04 KB
                        set |         170 |     39.11 KB
      weakref.ReferenceType |         392 |     27.56 KB
                  frozenset |          87 |     24.35 KB
image

Related: #570

from openadapt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.