Although other solutions are useful for a single function, I recommend the following piece of code where is more general and effective:
Rprof(tf <- "log.log", memory.profiling = TRUE)
# the code you want to profile must be in between
Rprof (NULL) ; print(summaryRprof(tf))