Memory profiling with lineprof shows you how to use the lineprof package to understand how memory is allocated and released in larger code blocks. Memory usage and garbage collection introduces you to the mem_used() and mem_change() functions that will help you understand how R allocates and frees memory.
Object size shows you how to use object_size() to see how much memory an object occupies, and uses that as a launching point to improve your understanding of how R objects are stored in memory. Along the way, you’ll learn about some common myths, such as that you need to call gc() to free up memory, or that for loops are always slow. The goal of this chapter is to help you understand the basics of memory management in R, moving from individual objects to functions to larger blocks of code.
It can even help you write faster code because accidental copies are a major cause of slow code. You’re reading the first edition of Advanced R for the latest on this topic, see the Names and values chapter in the second edition.Ī solid understanding of R’s memory management will help you predict how much memory you’ll need for a given task and help you to make the most of the memory you have.