Skip to main content

Python 3 memory analyzer for running processes

Project description

Python3 Memory Analyzer For Running Processes

Chasing down a memory leak? Don't want to add debugging code, or even stop your process?

You've come to the right place.

memory_analyzer

Running the memory analyzer does not require you to stop your process or add any special debugging code or flags. Running the analysis will not (cough should not) break your process, although your process (and all of its threads!) will be paused while the memory analyzer gathers information about the objects in memory.

License

This source code is licensed under the MIT license. Please see the LICENSE in the root directory for more information.

Things you can find out:

  1. How many of each object you have in memory

  2. The total size of all of these objects

  3. The forward references to the objects

  4. The backwards references to the objects

  5. The difference in size/number of objects over time (through snapshot comparisons)

Example Output:

Example Output Screenshot

How To Run

  1. Find the PID of your process:

    ps aux | grep <your process>
    
  2. Run the memory analyzer against it:

    memory_analyzer run $PID
    

This will put you into an ncurses UI and make a binary output file located in memory_analyzer_out/memory_analyzer_snapshot-{TIMESTAMP}

If you are analyzing a process that was run as root, you may need to run the analyzer as root as well.

Running on Ubuntu

In Maverick Meerkat (10.10) Ubuntu introduced a patch that disallows ptracing of non-child processes by non-root users. You can either look into disabling this restriction, or run the analyzer as root.

View the Output without Re-Analyzing

If you exit out of the UI but want to view the snapshot data again without re-running the analysis, you can use the 'view' command.

memory_analyzer view <snapshot output file>

Analyze Multiple Processes

You can analyze multiple processes at once by simply providing a list of the PIDs separated by spaces.

memory_analyzer run 1234 4567 890

If --snapshot is used, it tries to pair up the listed PIDs with the PIDs in the snapshot file. If they do pair, a new page is created for each like PID comparing the old and the new version. If it can't find any PIDs that pair up it just compares the first new and first old object.

If the references flags are used the references are found for all of the PIDs listed.

Viewing Forwards and Backwards References

You can either view the top N number of objects (sorted by size of object), or you can look for specific objects. The memory analyzer will generate PNG files displaying the graph, and will upload them for you to phabricator for easy viewing. If you don't want them uploaded you can use the —no-upload flag.

WARNING: Getting the references is a costly operation! The more references you grab the longer your process will remain paused for.

View the Top 2 Objects

memory_analyzer run -s 2 $PID

View specific objects

To view a specific object you must know it's fully qualified domain name. If you are unsure, I'd recommend running the analyzer first with no flags and identifying the names of the objects you are most interested in there.

memory_analyzer run $PID -ss __main__.Foo -ss builtins.str

This will get the forwards and backwards references of the Foo and str objects.

Comparing Snapshots

Comparing snapshots can be useful to show you how objects grow over time. To create an initial snapshot, just run the analyzer normally:

memory_analyzer run $PID --snapshot <previous snapshot file>

By default your snapshot files will all be saved in memory_analyzer_out/. The snapshot analysis will be located on the second page of the ncurses UI, so hit the arrow key to the right to scroll to the snapshot page once you are in the UI.

Specify the executable

The memory analyzer launches GDB with the executable found in sys.executable. This might not be the executable you want to use to analyze your binary. For example, you may need to use the debuginfo binary. You can specify the executable with the -e flag:

memory_analyzer run $PID -e /usr/local/bin/python3.7-debuginfo

Custom output file

If you don't want the file to be the default name, pass in your own custom name.

 memory_analyzer run $PID -f test_output1

Navigating the UI

Navigating the UI should be relatively intuitive. You can use the arrow keys or wasd to scroll up and down.

Scroll Up: Up arrow key or 'w'

Scroll Down: Down arrow key or 's'

Switch to next page: Right arrow key or 'd'

Switch to previous page: Left arrow key or 'a'

Page Up: Page Up key

Page Down: Page Down Key

Jump to Bottom of page: G

Jump to Top of page: H

Exit: 'q'

Dealing with Multithreading

Right now the program does NOT run in non-stop mode, meaning that if you have multiple threads they will all be paused during analysis. This could be a problem for some services that require threads to be completing live in the background. It might be possible to support non-stop mode (where only one thread gets paused), but it will only work if the process is stopped and started again by the memory analyzer (this is a limitation of GDB). Please contact me (lisroach) if you are interested in this feature.

Importantly, this means debugging multithreaded processes goes much slower than single threaded- because it takes the time to find and pause each individual thread. If your process has timeouts that could be triggered by a paused thread (e.g. something that talks over the network), use the memory_analyzer with caution.

FAQ

No data returned

Try running the process with the --debug flag enabled for more information. If there are no obvious errors occurring, your program is likely hung and cannot be analyzed.

No symbol table is loaded. Use the "file" command.

This is an error from GDB itself, and it could mean a couple of things:

  1. You don't have the correct debuginfo installed, or
  2. You are using the wrong Python executable.

To fix the incorrect debuginf install the debuginfo associated with the python runtime the analyzed process is using. For example:

sudo yum install python3-debuginfo

or sudo apt-get install python3-dbg

To solve the wrong Python executable, figure out what executable you need (which python version the analyzed process is running with), and use the --exec flag to specify it:

memory_analyzer run $PID -e /usr/local/bin/python3.6

It is hanging

Try running the memory_analyzer with the --debug flag. This is going to show you what GDB is currently doing.

You should not see:

$5 = <some number>

or

Inferior Detached

These are problems. Please file a bug. The memory_analyzer is a new tool, so not all scenarios have been tested and you may have run into something new. Keep reading for more information on understanding the debug output.

If you see:

GDB Stopping Many Threads

That means your process has a lot of threads, and it's probably taking GDB a long time to pause them all.

After this you will see:

Thread Debugging Finished

That means GDB is still working on doing GDB things, loading symbols and what not. This is usually what takes the longest, and as of right now I am not sure how to fix it. If you were to run gdb directly and attach to your process, this would take just as long :(

It is not until you see:

Detached

That the analyzer itself is running. Generally this runs very fast and you should barely see these messages flicker by.

If you don't see the above, but you do see:

Analyzing and Detached

This usually means you are not analyzing the correct process. Perhaps your process is not Python (maybe you analyzed while buck is building)?

Does it run on Windows

No. If you are interested in having this available on Windows it should be possible to add this, let lisroach know.

Does it work with Python 2

No, unfortunately this is Python 3 exclusive and is likely to stay that way.

Development

Any help is welcome and appreciated!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

memory_analyzer-0.1.1.tar.gz (17.6 kB view details)

Uploaded Source

Built Distribution

memory_analyzer-0.1.1-py3-none-any.whl (19.7 kB view details)

Uploaded Python 3

File details

Details for the file memory_analyzer-0.1.1.tar.gz.

File metadata

  • Download URL: memory_analyzer-0.1.1.tar.gz
  • Upload date:
  • Size: 17.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.5

File hashes

Hashes for memory_analyzer-0.1.1.tar.gz
Algorithm Hash digest
SHA256 2270f4978c939592fc1045e8ebfc51b41f2c43281bab79a3856aba078538914a
MD5 8034950a25ef4043be0dcc5c9972d530
BLAKE2b-256 5c62cd4a233604e367308340f67a605b63bdfc55118b0cdc82c8bd6f5389c0c4

See more details on using hashes here.

File details

Details for the file memory_analyzer-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: memory_analyzer-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 19.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.5

File hashes

Hashes for memory_analyzer-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6b6ebe007646ec7bdc484feae291188e5fa987f42349a22a6aec58afcf1b81aa
MD5 350b769595970a321ad7acf7ed57e336
BLAKE2b-256 e87b6bb669d8582c577d7bfd414654b77709f35ff612fb3903a63760fea86258

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page