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

Fix PYTHONTRACEMALLOC env var behavior #12

Merged
merged 4 commits into from Oct 15, 2018

Conversation

Projects
None yet
2 participants
@carsonip
Copy link

carsonip commented Oct 8, 2018

Document tracemalloc.start nframe argument usage as noted in API page.

Update index.rst about traceback frame count
Document `tracemalloc.start` `nproc` argument usage as noted in API page.
@vstinner
Copy link
Owner

vstinner left a comment

I don't understand why you removed the PYTHONTRACEMALLOC environment variable: patches for Python 2.7.x use this variable to enable tracemalloc at startup.

@carsonip

This comment has been minimized.

Copy link
Author

carsonip commented Oct 10, 2018

I tried to use PYTHONTRACEMALLOC but it didn't work. That's why I assume the env var isn't working. Now I look into the logs and find this:

failed to start tracemalloc:
ImportError: No module named _tracemalloc

If I start the patched python and import _tracemalloc, there is no problem. I guess we just cannot import _tracemalloc when the interpreter starts.

Also, in the API docs, it says:

There are differences between the third party pytracemalloc module (downloaded from PyPI) and the tracemalloc which is part of the Python standard library:
stdlib tracemalloc supports a PYTHONTRACEMALLOC environment variable to start tracing at Python startup.

Therefore I am under the impression that PYTHONTRACEMALLOC never work. I can only get things working with tracemalloc.start() with nframe. Please let me know if I did anything wrong. If we confirm that PYTHONTRACEMALLOC does not work, we should clearly document it.

@vstinner

This comment has been minimized.

Copy link
Owner

vstinner commented Oct 10, 2018

I tried to use PYTHONTRACEMALLOC but it didn't work.

How did you try pytracemalloc? The installation requires to patch the Python source code and compile it manually:
https://pytracemalloc.readthedocs.io/install.html

@carsonip

This comment has been minimized.

Copy link
Author

carsonip commented Oct 10, 2018

Yes. I tried with multiple Python versions too. I wrote a blog post here. The part about 2.7.9 and pip is wrong. I'll correct it tonight. But yes, I patched, compiled and installed pytracemalloc with and without virtualenv.

@vstinner

This comment has been minimized.

Copy link
Owner

vstinner commented Oct 11, 2018

It seems like you used https://github.com/vstinner/pytracemalloc/blob/master/patches/2.7/pep445.patch and this patch checks PYTHONTRACEMALLOC env var, but it's done before initializing the site module. Importing _tracemalloc should work before loading site. You can try using:

# -S: don't import site
/opt/tracemalloc/py27/bin/python2.7 -S -c "import tracemalloc"
@vstinner

This comment has been minimized.

Copy link
Owner

vstinner commented Oct 11, 2018

@carsonip

This comment has been minimized.

Copy link
Author

carsonip commented Oct 14, 2018

It seems like you used https://github.com/vstinner/pytracemalloc/blob/master/patches/2.7/pep445.patch and this patch checks PYTHONTRACEMALLOC env var, but it's done before initializing the site module. Importing _tracemalloc should work before loading site. You can try using:

# -S: don't import site
/opt/tracemalloc/py27/bin/python2.7 -S -c "import tracemalloc"

No, it doesn't work with the -S. It works without -S. Never worked with PYTHONTRACEMALLOC env var.

Here's the output with -S.

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "tracemalloc.py", line 8, in <module>
    from _tracemalloc import *
ImportError: No module named _tracemalloc

http://carsonip.me/posts/debugging-memory-usage-python-tracemalloc/

Nice. Did you find your memory leak? :-)

I wish. I am seeing increasing memory usage in the order of GB in top but steady and reasonable objects in order of MB in tracemalloc. Still looking into it.

@vstinner

This comment has been minimized.

Copy link
Owner

vstinner commented Oct 15, 2018

No, it doesn't work with the -S. It works without -S. Never worked with PYTHONTRACEMALLOC env var.

Ok, so it's a bug in the patch. The fix is not to change the doc, but fix the patch.

The test on PYTHONTRACEMALLOC env var must be done after initsite(), not before.

@carsonip

This comment has been minimized.

Copy link
Author

carsonip commented Oct 15, 2018

Ok, so it's a bug in the patch. The fix is not to change the doc, but fix the patch.

The test on PYTHONTRACEMALLOC env var must be done after initsite(), not before.

I'll look into it soon.

@carsonip

This comment has been minimized.

Copy link
Author

carsonip commented Oct 15, 2018

Thanks for the tips. Now I've updated the patches, added a testing script for patches, and fixed the documentation. Please let me know your thoughts. Also, is it possible to cut a new release?

@carsonip carsonip changed the title Update index.rst about traceback frame count Fix PYTHONTRACEMALLOC env var behavior Oct 15, 2018

@vstinner vstinner merged commit d8676f8 into vstinner:master Oct 15, 2018

@vstinner

This comment has been minimized.

Copy link
Owner

vstinner commented Oct 15, 2018

Thanks. Fixing the patches is the correct fix, not removing PYTHONTRACEMALLOC from the doc.

Amazing job, it's really annoying to maintain these patches, but it seems like you generated properly these patches! Thanks again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment