mem.tally.report(message) mem.tally.reset() print(mem.tally.report())
print.mem.tally
.
mem.tally.report
returns two numbers:
the first is always 0 and the second is
the maximum amount of memory asked for at one time by S-PLUS since
the last call to
mem.tally.reset
.
If the argument
message
was used it attaches that as an attribute.
It gives the result the class
"mem.tally"
so that
print.mem.tally
will be called by
print
to format its output.
The evaluation memory reported is the maximum amount of memory
simultaneously allocated by the C function
S_alloc
since the last
call to
mem.tally.reset
.
S_alloc
is used to allocate space for
all S-PLUS objects in the evaluator and is used to allocate most
scratch space needed during evaluation.
It is not used for any GUI-related computations.
In previous versions of S-PLUS, the first number returned told
how much memory had been used to cache objects when they were
read from disk. Now it is always 0 as it is hard to distinguish
this memory from other memory used during evaluation.
S-PLUS Programmers Manual.
# here we have a function that seems to use too much # memory. To diagnose the problem we call mem.tally.reset # at the beginning and sprinkle calls to print(mem.tally.report()) # through the function. f <- function(n) { mem.tally.reset() on.exit(print(mem.tally.report("Leaving function"))) x <- runif(n) y <- runif(n) print(mem.tally.report("Just created x and y")) num <- sum(outer(x, y, ">")) print(mem.tally.report("Just computed num")) num } set.seed(1) f(100) # Produces output that looks like: # Just created x and y # new database evaluation # 0 2161 # Just computed num # new database evaluation # 0 242653 # Leaving function # new database evaluation # 0 242653 # [1] 4636 # We see the memory usage jumped when computing sum(outer()). # Replace sum(outer()) by sum of sums. f2 <- function(n) { mem.tally.reset() on.exit(print(mem.tally.report("Leaving function"))) x <- runif(n) y <- runif(n) print(mem.tally.report("Just created x and y")) num <- sum(unlist(lapply(y, function(yi, x) sum(x > yi), x))) print(mem.tally.report("Just computed num")) num } set.seed(1) f2(100) # Now it produces output like # Just created x and y # new database evaluation # 0 2161 # Just computed num # new database evaluation # 0 85561 # Leaving function # new database evaluation # 0 85561 # [1] 4636 # Much better. Next should use dos.time or unix.time to # see if this change slows it down.