Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Python Memory Management

Junseong
December 15, 2021
670

Python Memory Management

2021.12.15 스캐터랩 엔지니어링 세미나에서 발표한 Python Memory Management 발표자료 입니다.

Junseong

December 15, 2021
Tweet

Transcript

  1. Python Memory Management Reference Count 10 A Reference Object B

    Reference Count: 2 ࢜۽਍ чਸ Memory ী Allocation ೞח Ѫ੉ ইצ ӝઓ Object ী Reference ୶о
  2. Python Memory Management Reference Count 10 A Object B Reference

    Count: 1 (-1) पઁ A ੄ Object о Dealloc غח Ѫ੉ ইצ Reference ݅ ࢏ઁ
  3. Python Memory Management if Reference Count == 0 10 A

    Object B Reference Count: 0 (-1) -> deallocation
  4. Python Memory Management Ӓۢ Garbage Collection ੉ ೙ਃ হחѢ ইצоৈ??

    ࢎप Python ীࢲח ਤ җ੿ਸ Reference Count based GC ۄҊ ࠗܰҊ ੓णפ׮.
  5. [L] Python Memory Management Recursive Reference L Object Reference Count:

    2 ੉۠ ࣽജ ଵઑ غয ੓ח ҃਋ Reference Count ܳ ӝ߈ਵ۽ ೠ Deallocation ੉ ࠛоמೞ׮
  6. [L] Python Memory Management Cyclic Reference L Object Reference Count:

    1 ੿׹: Name L ਸ ૑ਕب ࣽജଵઑ غয ੓ӝ ٸޙী Reference Count ח ੺؀ 0 ੉ غ૑ ঋח׮ => Memory Leak!
  7. Python Memory Management GC for Cyclic Reference Python ীࢲ GCח

    Cyclic Reference ܳ dealloc ೞחؘ ౠചغয ੓णפ׮.
  8. Foo(0) Python Memory Management GC for Cyclic Reference A Object

    Next Foo(1) B Next Foo(0) Ref Count : 2 Foo(1) Ref Count : 2
  9. Python Memory Management GC Reference Count Object Reference Count GC_Reference

    Count Reason Foo(0) 2 2 copy(refCnt) Foo(1) 2 2 copy(refCnt)
  10. Python Memory Management Automated Garbage Collection Object Reference Count GC_Reference

    Count Reason Foo(0) 2 1 Foo(1).next.gcRefCnt -= 1 Foo(1) 2 1 Foo(0).next.gcRefCnt -= 1 Nothing Happened!
  11. Foo(0) Python Memory Management GC for Cyclic Reference A Next

    Foo(1) B Next Foo(0) Ref Count : 1 Foo(1) Ref Count : 1
  12. Python Memory Management GC Reference Count Object Reference Count GC_Reference

    Count Reason Foo(1) 1 1 copy(ref_count) Foo(2) 1 1 copy(ref_count) Reference Count ч Deep Copy
  13. Python Memory Management GC Reference Count Object Reference Count GC_Reference

    Count Reason Foo(0) 1 0 Foo(1).next.gcRefCnt -= 1 Foo(1) 1 0 Foo(0).next.gcRefCnt -= 1
  14. Python Memory Management Cyclic Reference ী ؀ೠ GC ࢿҕ!! Object

    Reference Count GC_Reference Count Reason Foo(0) 1 0 Foo(1).next.gcRefCnt -= 1 Foo(1) 1 0 Foo(0).next.gcRefCnt -= 1 Dealloc Dealloc
  15. Python Memory Management ୨ 3ѐ੄ Reference Count о хࣗ غযঠ

    ೞחؘ, Ѿҗ੸ਵ۽ח 2ѐ݅ хࣗؽ -> Race Condition Ѿҗ੸ਵ۽ memory leak ਸ ߊࢤदఃѢա ߈؀۽ ই૒ ࢓ই੓যঠ ೡ objectܳ લৈߡܾ ࣻب ੓׮.
  16. Python Memory Management ੉Ѧ ߑ૑ೞ۰ݶ ݽٚ Reference Count Operation ী

    Lock ਸ Ѧݶ ؽ ೞ૑݅ Python ਷ Ѣ੄ ݽٚ Operation ীࢲ Reference Count ܳ ೞӝ ٸޙী ੉ח ষ୒դ ࠺ബਯࢿਸ оઉ১
  17. Python Memory Management Reference Counting ਸ ӝ߈ਵ۽ ೠ GC ܳ

    ࢎਊೞח ҃਋ Thread-Safe ೧ঠ ೣ Ӓ Ѿҗ GIL ߆ী ؀উ੉ হח Ѫ…. -> Ӓېࢲ Java ח Mark and Sweep ߑध੄ GC ܳ ࢎਊ೤פ׮ ੉Ѥ ׮ܲ ࠙੉..^^7
  18. Python Memory Management ࢎप ݫݽܻ ҕрী ҙೠ ղਊب ੓חؘ.. -

    Pool, Block, Arena, Generation ١١… ੉۞׮р ೞܖઙੌ ೧ঠ ೧ࢲ ׮਺ ӝഥী..ƃƃ
  19. Python Memory Management ࠗ۾: ৵ Python ਷ String Overhead о

    ੓חо? ౵੉ॆ਷ ‘ч’੉ۄח ѐ֛੉ হ਺ ݽٚ૑ Object - Reference Count - Type Info - Char Array (value) - Intern ച غয ੓ח૑ ৈࠗ - String Object ੄ Hash ч
  20. хࢎ೤פ׮✌ ୶о ૕ޙ ژח ҾӘೠ ੼੉ ੓׮ݶ ঱ઁٚ ইې োۅ୊۽

    োۅ ઱ࣁਃ! ӣળࢿ (ML Research Engineer, Pingpong) [email protected] Facebook. @codertimo Linked in. @codertimo Github. @codertimo