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

#100DaysOfPyCon2018 Day2: There and Back Again: Disable and re-enable garbage collector at Instagram
Photo by Sigmund / Unsplash

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 ด้วย ไปอ่านต่อกันได้

Copy-on-write friendly Python garbage collection
At Instagram, we have the world’s largest deployment of the Django web framework, which is written entirely in Python. We began using…

Original post at: https://yothinix.medium.com/100daysofpycon2018-day2-there-and-back-again-disable-and-re-enable-garbage-collector-at-136b6103bfb4