Talks
Speakers
Events
Topics
Sign in
Home
Talks
Speakers
Events
Topics
Leaderboard
Use
Analytics
Sign in
Suggest modification to this talk
Title
Description
Did you know that Ruby 2.7 introduces a new method for manual memory compaction? Neither did I. Then a user reported a bug on a gem I maintain, and well... In this talk, I’ll tell you a story about how one bug forced me to learn all about memory management in Ruby. By the end of this talk, you should understand how memory is allocated on the heap, how Ruby implements garbage collection, and what memory compaction is all about! Emily Giurleo Emily Giurleo is a software engineer and avid Rubyist. This December, she'll start working at Numero, where she'll help build the next generation of campaign finance tools. In her spare time, she enjoys creating tech for good causes, reading fantasy novels, and hanging out with her pets.
Date
Summarized using AI?
If this talk's summary was generated by AI, please check this box. A "Summarized using AI" badge will be displayed in the summary tab to indicate that the summary was generated using AI.
Show "Summarized using AI" badge on summary page
Summary
Markdown supported
In the talk titled "The Bug that Forced Me to Understand Memory Compaction," Emily Giurleo, a software engineer and Rubyist, discusses her journey of learning about memory management in Ruby after encountering a bug related to the BSON gem she maintained. The presentation outlines several key concepts concerning Ruby's memory allocation, garbage collection, and the newly introduced memory compaction feature in Ruby 2.7. ### Key Points Discussed: - **Starting Point**: Giurleo's knowledge of Ruby's garbage collection was limited until a user reported a segfault error associated with `gc.compact` in the BSON gem, a C extension used for serializing data for MongoDB. - **Understanding Memory and the Heap**: She explains that memory is stored in RAM, specifically within what Ruby calls the Ruby heap, which is organized into slots on heap pages. Each object in Ruby occupies a slot in this heap while the program runs. - **Garbage Collection (GC)**: Ruby employs a mark-and-sweep algorithm for garbage collection, where unused objects are marked and subsequently swept away to free up memory. Despite this, freed slots can lead to memory bloat since they cannot be easily reused if they are not contiguous. - **Memory Compaction**: The `gc.compact` method introduced in Ruby 2.7 allows for memory compaction. This process moves used memory slots to the start of the heap, thereby freeing up space at the end for future allocations. Giurleo likens effective memory management to organizing clutter, much like Marie Kondo. - **Interplay with C Extensions**: Issues arise when using memory compaction and C extensions because the addresses used in C must align with Ruby's memory structure. Giurleo outlines that if the Ruby garbage collector does not know about pointers and their relationships, memory issues, including segmentation faults, can occur. ### Resolving the Segfault: - She discovered that her BSON gem failed to mark a long-lived object, leading to mishandling during garbage collection, which caused the segfault. The solution involved using `rb_gc_register_mark_object` to properly mark long-lived variables as root objects, preventing them from being relocated during compaction. - Giurleo emphasizes two main lessons drawn from this experience: the importance of understanding systems design in addition to coding skills, and the value of knowledge sharing in the tech community for overcoming challenges. ### Conclusion: - Giurleo concludes with essential takeaways regarding memory management in Ruby 2.7 and offers advice on implementing C extensions for better compatibility and function. She encourages developers to explore and share knowledge, underscoring the collective growth of the developer community.
Suggest modifications
Cancel