DEV Community

Discussion on: Tight Code 1: When immutability goes bad

Collapse
 
he_zhenghao profile image
Zhenghao He

Thanks for the post! Learned a lot. Just curious about how you arrived at the number 7GB when the message list is using 1.4mb in memory? Could you elaborate on that and the way you calculated it or measured it?

Collapse
 
miketalbot profile image
Mike Talbot ⭐ • Edited

Each time you add a single item to the array, you allocate a whole new array. So it's a factorial sequence. Do it 10,000 times from 0 - 10k and the memory allocated along the way is 7GB. Of course each iteration the previous array is available for garbage collection and is collected, but there you go - the effort of garbage collecting and allocating new space isn't free etc! It can also easily lead to memory fragmentation which requires more complex GC.

Collapse
 
he_zhenghao profile image
Zhenghao He

Hi thanks for the explanation! I would love to know more about how you can profile the memory allocation to quantitatively analyze the performance problem. Are there any good resources on this?