40 Memory

40.1 Object size

  1. Q: Repeat the analysis above for numeric, logical, and complex vectors.





  2. Q: If a data frame has one million rows, and three variables (two numeric, and one integer), how much space will it take up? Work it out from theory, then verify your work by creating a data frame and measuring its size.

    A: From the textbook we know that

    • an integer’s size is 40 bytes plus 4 bytes per allocated entry,
    • a numerics’s size is 40 bytes plus 8 bytes per allocated entry.

    So we can calculate the size via:

    object_size(df) = 1 * (40 + 4 * 1,000,000) + 2 * (40 + 8 * 1,000,000) = 20,000,120 bytes.

    And test this via:

    Note that we observe a small difference, because we didn’t include the costs for creating the data.frame() (560 bytes) in our previous calculations.

  3. Q: Compare the sizes of the elements in the following two lists. Each contains basically the same data, but one contains vectors of small strings while the other contains a single long string.


  4. Q: Which takes up more memory: a factor (x) or the equivalent character vector (as.character(x))? Why?

    A: To be exact: it depends on the length of unique elements in relation to the overall length of the vector.

    • In case of a long vector with only a few levels, the character takes approximately twice the memory of a factor:

    That is, because a character allocates 8 bytes per entry (if the entry has less than 8 signs, otherwise roughly one byte per sign) and a factor equals an integer (allocates only 4 bytes per entry) with a character vector attribute that contains the levels (unique elements) of the vector:

    • Of course the factor will allocate more memory, if all entries are unique:
  5. Q: Explain the difference in size between 1:5 and list(1:5).

    A: An empty list needs 40 bytes. For each entry 8 bytes are added. We can see this via:

    Since 1:5 needs 72 bytes (note that for memory for short integers is allocated in chunks, as explained in the textbook), list(1:5) takes 120 bytes (48 + 72). So in general the cost for saving atomics within a list is 40 bytes for the list plus 8 bytes per atomic/list entry.

40.2 Memory profiling with lineprof

  1. Q: When the input is a list, we can make a more efficient as.data.frame() by using special knowledge. A data frame is a list with class data.frame and row.names attribute. row.names is either a character vector or vector of sequential integers, stored in a special format created by .set_row_names(). This leads to an alternative as.data.frame():

    What impact does this function have on read_delim()? What are the downsides of this function?

  2. Q: Line profile the following function with torture = TRUE. What is surprising? Read the source code of rm() to figure out what’s going on.

40.3 Modification in place

  1. Q: The code below makes one duplication. Where does it occur and why? (Hint: look at refs(y).)

    A: It occurs in the first iteration of the for loop. refs(y) is 2 before the for loop, because y is created via as.list(), which is not a primitive and so sets the refs counter up to two. Therefore R makes a copy, refs(y) becomes 1 and the following modifications will occur in place. The following code illustrates this behaviour, when run in the RGui. (Note that refs() will always return 2, when run in RStudio, as stated in the textbook. Note also, that you could detect this behaviour with the tracemem() function).

  2. Q: The implementation of as.data.frame() in the previous section has one big downside. What is it and how could you avoid it?