0

I am using a function to gather data, reorganise it and write it into a SQL database. Over the execution I read in various data.tables and assign data to various objects and write these objects then back to the SQL database.

The only thing I actually return from the function is a small status update on how long the function was running and possible error messages.

   agent      start_time          end_time             duration_minutes success error_msg return_msg
1: R_Script.R 2019-11-18 15:35:32 2019-11-18 15:36:58         1.433333    TRUE             Finished

I thought in R the environment of a function is just transitory, i.e. after execution the data used and accessed by the function is removed, nevertheless I do see that the object.size of the function increases from 393248 bytes after the initial sourcing in an empty environment to 2618752 bytes after the execution. Using pryr::object_size() the size of the function after execution is only reported to be 724kB and before execution 471kB.

The function works and all the data is stored on the file system via saveRDS and written to a database using odbc and DBI. I am basically just curious on what is causing the increasing size of the function. I couldn't find an explanation in the documents describing the different environments, here http://adv-r.had.co.nz/Environments.html#function-envs

hannes101
  • 2,410
  • 1
  • 17
  • 40
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) that can be used to test and verify possible solutions. – MrFlick Nov 18 '19 at 16:13
  • 1
    It's likely just-in-time compiling. See `?compiler::enableJIT` for details. Use `compiler::disassemble(fn)` to see the compiled version of the function. – user2554330 Nov 18 '19 at 16:55
  • A reproducible example is difficult, since it's mostly internal stuff and the function is quite long. Thanks for the hint with the just-in-time compiling, this might be the reason for the increase in size. – hannes101 Nov 19 '19 at 08:25

0 Answers0