Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

_abc._abc_subclasscheck has very poor performance and (I think) a memory leak #92810

Open
Labels
extension-modulesC modules in the Modules dirperformancePerformance or resource usagetype-bugAn unexpected behavior, bug, or error
@samuelcolvin

Description

@samuelcolvin

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 anything that bad in a minimal example, but I have the following:

fromabcimportABCMetafromdatetimeimportdatetimeclassMyMetaclass(ABCMeta):passclassMyClass(metaclass=MyMetaclass):passdefmain():classFoobar(MyClass):passassertissubclass(Foobar,MyClass)assertnotissubclass(int,MyClass)assertnotissubclass(str,MyClass)assertnotissubclass(datetime,MyClass)t=type('A', (), {})assertnotissubclass(t,MyClass)if__name__=='__main__':importos,psutilprocess=psutil.Process(os.getpid())mb=1024*1024last=0foriinrange(5_000):main()# mem = process.memory_info().rss# print(f'{i + 1:>4d} {mem / mb:8.2f}MB {(mem - last) / mb:+8.2f}MB | {"━" * int(mem / 8_000_000)}')# last = mem

A few things to note:

  • the commented out last few lines print current memory and change in memory over time
  • I'm not sure exactly whichissubclass calls are necessary, certainly I don't see a rise in memory with just the last one
  • some memory drops (presumably related to clearing the abc caches?) is happening, but overall memory is increasing
  • The performance ofissubclass on an abc is very poor indeed - this script takes 14seconds, if i switchABCMeta totype it takes 121ms!

Your environment

  • Python 3.10.0 installed with venv
  • Ubuntu 21.10

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirperformancePerformance or resource usagetype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp