Title: `_abc._abc_subclasscheck` has very poor performance and (I think) a memory leak · Issue #92810 · python/cpython · GitHub
Open Graph Title: `_abc._abc_subclasscheck` has very poor performance and (I think) a memory leak · Issue #92810 · python/cpython
X Title: `_abc._abc_subclasscheck` has very poor performance and (I think) a memory leak · Issue #92810 · python/cpython
Description: Bug report I'm been hunting a memory leak in pydantic and I keep seeing _abc_subclasscheck popping up in the memray flamegraph. Locally I've seen memray report that _abc_subclasscheck is using 2.8GB of memory in some cases! I can't get a...
Open Graph Description: Bug report I'm been hunting a memory leak in pydantic and I keep seeing _abc_subclasscheck popping up in the memray flamegraph. Locally I've seen memray report that _abc_subclasscheck is using 2.8G...
X Description: Bug report I'm been hunting a memory leak in pydantic and I keep seeing _abc_subclasscheck popping up in the memray flamegraph. Locally I've seen memray report that _abc_subclasscheck is us...
Opengraph URL: https://github.com/python/cpython/issues/92810
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`_abc._abc_subclasscheck` has very poor performance and (I think) a memory leak","articleBody":"**Bug report**\r\n\r\nI'm been hunting a memory leak in pydantic and I keep seeing `_abc_subclasscheck` popping up in the memray flamegraph.\r\n\r\nLocally I've seen memray report that `_abc_subclasscheck` is using 2.8GB of memory in some cases!\r\n\r\nI can't get anything that bad in a minimal example, but I have the following:\r\n\r\n```py\r\nfrom abc import ABCMeta\r\nfrom datetime import datetime\r\n\r\n\r\nclass MyMetaclass(ABCMeta):\r\n pass\r\n\r\n\r\nclass MyClass(metaclass=MyMetaclass):\r\n pass\r\n\r\n\r\ndef main():\r\n class Foobar(MyClass):\r\n pass\r\n\r\n assert issubclass(Foobar, MyClass)\r\n assert not issubclass(int, MyClass)\r\n assert not issubclass(str, MyClass)\r\n assert not issubclass(datetime, MyClass)\r\n t = type('A', (), {})\r\n assert not issubclass(t, MyClass)\r\n\r\n\r\nif __name__ == '__main__':\r\n import os, psutil\r\n process = psutil.Process(os.getpid())\r\n mb = 1024 * 1024\r\n last = 0\r\n for i in range(5_000):\r\n main()\r\n # mem = process.memory_info().rss\r\n # print(f'{i + 1:>4d} {mem / mb:8.2f}MB {(mem - last) / mb:+8.2f}MB | {\"━\" * int(mem / 8_000_000)}')\r\n # last = mem\r\n```\r\n\r\nA few things to note:\r\n* the commented out last few lines print current memory and change in memory over time\r\n* I'm not sure exactly which `issubclass` calls are necessary, certainly I don't see a rise in memory with just the last one\r\n* some memory drops (presumably related to clearing the abc caches?) is happening, but overall memory is increasing\r\n* The performance of `issubclass` on an abc is very poor indeed - this script takes 14seconds, if i switch `ABCMeta` to `type` it takes 121ms!\r\n\r\n**Your environment**\r\n\r\n* Python 3.10.0 installed with venv\r\n* Ubuntu 21.10\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-131914\n* gh-141171\n* gh-144941\n* gh-150540\n* gh-162361\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/samuelcolvin","@type":"Person","name":"samuelcolvin"},"datePublished":"2022-05-14T21:08:49Z","url":"https://github.com/python/cpython/issues/92810"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:64d81176-56cf-8448-6dd3-d7ace7ea87bc |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D1A4:29AEE8:31BEE64:42472BA:6B01EC2B |
| html-safe-nonce | 8fdb5214dc304c97364f7e5b9e86206505a11625d29082fd279e2162ffe53d9e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMUE0OjI5QUVFODozMUJFRTY0OjQyNDcyQkE6NkIwMUVDMkIiLCJ2aXNpdG9yX2lkIjoiNDA0NzA2MDkwMDcwNjkwNTEzMSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | ff97c1af67869072a43335af5927393426e7b7d31e59276910a5d55ebdabc43e |
| hovercard-subject-tag | issue:1236121997 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/92810/issue_layout |
| twitter:image | https://opengraph.githubassets.com/226c5f10f252a4df19c35e825b6319ddbdba8e60aa1a0c80fda4d86aea5595c4/python/cpython/issues/92810 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/226c5f10f252a4df19c35e825b6319ddbdba8e60aa1a0c80fda4d86aea5595c4/python/cpython/issues/92810 |
| og:image:alt | Bug report I'm been hunting a memory leak in pydantic and I keep seeing _abc_subclasscheck popping up in the memray flamegraph. Locally I've seen memray report that _abc_subclasscheck is using 2.8G... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | samuelcolvin |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width