Skip to content

Summary of Total Object.Size in R Script

7 messages · Gundala Viswanath, jim holtman, Brian Ripley +1 more

#
Dear all,

Is there a way we can find the total object.size of
all the objects in our R script?

The reason we want to do this because we want to know
how much memory does our R script require overall.

Rprofmem(), doesn't seem to do it.

and Unix 'top' command is dynamic and
it doesn't give the exact byte size.

- Gundala Viswanath
Jakarta - Indonesia
#
Here is a function I use to see how big the objects in my workspace are:
+ function (pos = 1, sorted = F)
+ {
+     .result <- sapply(ls(pos = pos, all.names = TRUE), function(..x)
object.size(eval(as.symbol(..x))))
+     if (sorted) {
+         .result <- rev(sort(.result))
+     }
+     .ls <- as.data.frame(rbind(as.matrix(.result), `**Total` = sum(.result)))
+     names(.ls) <- "Size"
+     .ls$Size <- formatC(.ls$Size, big.mark = ",", digits = 0,
+         format = "f")
+     .ls$Mode <- c(unlist(lapply(rownames(.ls)[-nrow(.ls)],
function(x) mode(eval(as.symbol(x))))),
+         "-------")
+     .ls
+ }
Size        Mode
.my.env         28 environment
.Random.seed 2,528     numeric
.required       72   character
my.ls        6,712    function
**Total      9,340     -------
On Tue, Jan 13, 2009 at 9:53 AM, Gundala Viswanath <gundalav at gmail.com> wrote:

  
    
#
On Tue, 13 Jan 2009, jim holtman wrote:

            
Be careful with the caveats spelled out in ?object.size.  Especially 
for character data such summations can be way off.

  
    
#
That is certainly true because I have seen differences due to the
sharing of values.  I also look at what 'gc()' shows at the memory
being used.  Does this provide a reasonable estimate of the total
space being used?

On Tue, Jan 13, 2009 at 11:14 AM, Prof Brian Ripley
<ripley at stats.ox.ac.uk> wrote:

  
    
#
On Tue, 13 Jan 2009, jim holtman wrote:

            
Yes, but not just by your workspace, also all the loaded packages.

  
    
1 day later
#
Sorry for my late reply.

Thank you so much Jim. This script of yours
is very2 useful. I have used it.

- Gundala Viswanath
Jakarta - Indonesia
On Wed, Jan 14, 2009 at 12:17 AM, jim holtman <jholtman at gmail.com> wrote:
#
See also ll() in the R.oo package, e.g.

# To list all objects in .GlobalEnv:
ll()
                  member data.class dimension objectSize
1                *tmp*     Person         1         428
2  as.character.Person   function      NULL        1208
3              country  character         1          44
4        equals.Person   function      NULL        2324
5             filename  character         1          84
6               getAge   function      NULL         372
7        getAge.Person   function      NULL         612
8       getName.Person   function      NULL         628
9      hashCode.Person   function      NULL        1196
10        last.warning       list         1         192
11                 obj     Person         1         428
12              Person      Class      NULL        2292
13              setAge   function      NULL         372
14       setAge.Person   function      NULL        2088
15             setName   function      NULL         372
16      setName.Person   function      NULL         760
17   staticCode.Person   function      NULL        2372

# To list all functions in the methods package:
ll(mode="function", envir="methods")

# To list all numeric and character object in the base package:
ll(mode=c("numeric", "character"), envir="base")

# To list all objects in the base package greater than 40kb:
subset(ll(envir="base"), objectSize > 40000)

ll() takes argument 'properties', which allows you to construct any
column property you ever like.  There is also a 'sortBy' argument.
See help(ll) for more details.

/Henrik
On Thu, Jan 15, 2009 at 3:00 AM, Gundala Viswanath <gundalav at gmail.com> wrote: