# Don’t Forget about Memory Management

- Speaker: [Shachar Langbeheim](/wiki/speakers/shachar-langbeheim.md)
- Event: [Reversim Summit 2021](/wiki/years/2021.md)
- Video: https://www.youtube.com/watch?v=HQiCPwfBWwQ
- Transcript: [/transcripts/2021/23-HQiCPwfBWwQ.md](/transcripts/2021/23-HQiCPwfBWwQ.md)
- Language: Hebrew · Type: talk · Level: intermediate
- Topics: [Performance](/wiki/topics/performance.md), [Software Engineering](/wiki/topics/software-engineering.md), [Infrastructure](/wiki/topics/infrastructure.md), [Java](/wiki/topics/java.md)

> Written by an LLM from machine transcripts. It can contain errors. Check the transcript before you quote it.

## Summary

This talk explores the fundamentals of memory management in software engineering, contrasting stack and heap memory and their performance implications. The speaker examines three main approaches to memory management: manual management, reference counting, and garbage collection. The audience learns how hardware caches and memory locality impact application performance, and when to combine different strategies for optimal results.

## Key points

- [2:26](https://www.youtube.com/watch?v=HQiCPwfBWwQ&t=146s) The stack stores thread-specific execution context and data, whereas the heap contains shared data accessible across all threads.
- [15:59](https://www.youtube.com/watch?v=HQiCPwfBWwQ&t=959s) CPU architecture and caching layers mean that accessing heap memory is significantly slower than working with data in registers or CPU caches.
- [31:55](https://www.youtube.com/watch?v=HQiCPwfBWwQ&t=1915s) Achieving locality of reference ensures that data accessed together resides close to each other in memory, greatly improving performance.
- [37:17](https://www.youtube.com/watch?v=HQiCPwfBWwQ&t=2237s) Manual memory management provides optimal performance tailored to specific application needs, but introduces high risk for bugs like memory leaks and crashes.
- [48:47](https://www.youtube.com/watch?v=HQiCPwfBWwQ&t=2927s) Reference counting automates memory reclamation by tracking pointers, but it adds atomic operation overhead and can lead to retain cycles.
- [48:47](https://www.youtube.com/watch?v=HQiCPwfBWwQ&t=2927s) Garbage collection simplifies development by automatically freeing unreachable objects, though it introduces unpredictable pauses and performance overhead.
- [48:47](https://www.youtube.com/watch?v=HQiCPwfBWwQ&t=2927s) Developers can optimize resource-heavy applications by combining idiomatic platform code with data-oriented design and custom allocation strategies like object pools.

## Takeaways

- Understand the trade-offs between manual memory management, reference counting, and garbage collection.
- Design data structures with memory locality in mind to maximize CPU cache efficiency.
- Use custom object pools or manual allocation for heavy resources when standard garbage collection or reference counting causes performance bottlenecks.

## Questions this talk answers

- How does CPU caching affect the performance of heap allocations versus stack allocations?
- What are the primary differences between reference counting and garbage collection in terms of runtime overhead?
- When should a developer choose manual memory management over automated approaches?

## Technologies

C, C++, Rust, Swift, Python, JavaScript, WebAssembly

## Tags

cpu cache, garbage collection, memory management, performance, stack and heap, systems programming
