#100DaysOfPyCon2018 Day2: There and Back Again: Disable and re-enable garbage collector at Instagram

Yothin Muangsommuk
1 min readMay 16, 2018

--

Key takeaways
— Garbage collector (GC) จะทำงานอยู่บนหลักการสองอย่างคือ Reference count กับ Copy-On-Write Semantic

- Instagram disable GC ช่วยลด memory ได้จาก 500mb / process เหลือ 350mb / process (ลองคิดภาพ uWSGI ลดได้ขนาดนั้นแล้วรันซัก 100 กว่า process)

- ใช้ perf ในการทำ profiling memory usage

- Disable โดยใช้คำสั่ง gc.disable() ไม่เกิดอะไรขึ้น แต่ใช้ gc.set_threshold(0) ปิดได้จริงแต่สิ่งที่เกิดขึ้นคือ Instagram down เพราะ OOM ต้องแก้โดย hack atexit.register(os._exit, 0) แต่ปัจจุบันไม่ต้องแก้แล้วเพราะ hack นี้รวมไปใน Python 3.6 แล้ว

- หลังจากปิด GC ไป Memory utilize ลดลง 15% มี shared memory เพิ่มขึ้น 100mb และ instruction / cycle เพิ่มขึ้น 10%

- หลังจากปิดไปได้ซักพักเริ่มมีปัญหาเพราะ cyclic reference ป้องกันได้ยากมากในทีมที่โค้ดและขนาดทีมโตเร็วมากๆ อย่าง Instagram ทำให้ถึงแม้ว่าจะลด memory utilize ไปได้ แต่ขนาด process กลับโตขึ้นแทนจาก 600mb เป็น 1200mb ในปีเดียว

- ทีม Instagram เริ่มมองที่ GC COW ว่าจะทำยังไงให้มันมีประสิทธิภาพมากขึ้นถ้าเปิด GC เลยจบลงด้วยการออกแบบ PYGC_HEAD ใหม่ โดยย้ายเอามาเก็บใน memory pool แล้วใช้ poiter reference หา memory page อื่น แต่ก็ต้องเสีย 16kb เอาไว้ทำ pointer ต่อ object

- แทนที่จะย้าย PYGC_HEAD เอามาเก็บในใน memory page อันใหม่แล้วเสีย 16kb ทีม Instgram เลือกที่จะใช้ gc.freeze() ตัว memory page ก่อนที่จะ fork ไปสร้าง memory page ให้ object ใหม่

Wrap up
— disable garbage collection to avoid copy-on-write
— re-enable garbage collection by freezing objects
— no GC before fork to avoid copy on write reallocation from memory pool

หนักหน่วงมาก session นี้มี blog ด้วย ไปอ่านต่อกันได้

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Yothin Muangsommuk
Yothin Muangsommuk

Written by Yothin Muangsommuk

Pythonista @ProntoTools ♥ Python, Django, Vim and Star Trek 🖖

No responses yet

Write a response