Skip to content

object.size vs lobstr::obj_size

9 messages · Tomas Kalibera, Hadley Wickham, Hervé Pagès +1 more

#
On 2/19/20 3:55 AM, Stefan Schreiber wrote:
Please keep in mind that "actual size used in memory" is an elusive 
concept, particularly in managed languages such as R. Even in native 
languages, you have on-demand paging (not all data in physical memory, 
some may be imputed (all zeros), some may be swapped out, some may be 
stored in files (code), etc). Also you have internal and external 
fragmentation caused by the "C library" memory allocator, overhead of 
object headers and allocator meta-data. On top of that you have the 
managed heap: more of internal and external fragmentation, more headers. 
Moreover, memory representation may change invisibly and sometimes in 
surprising ways (in R it is copy-on-write, so the sharing, but also 
compact objects via ALTREP, e.g. sequences). R has the symbol table, 
string cache (strings are interned, as in some other language runtimes, 
so the price is paid only once for each string). In principle, managed 
runtimes could do much more, including say compression of objects with 
adaptive decompression, some systems internally split representation of 
large objects depending on their size with additional overheads, systems 
could have some transparent de-duplication (not only for strings), some 
choices could be adaptive based on memory pressure. Then in R, packages 
often can maintain memory related to specific R objects, linked say via 
external pointers, and again there may be no meaningful way to map that 
usage to individual objects.

Not only that what is a size of an object tree is not easy to define. 
That information is in addition not very useful, either, because 
innocuous changes may change it in arbitrary ways out of control of the 
user: there is no good intuition how much that size will change from 
intended application-level modifications of the tree. Users of the 
system could hardly create a reliable mental model of the memory usage, 
because it depends on internal design of the virtual machine, which in 
addition can change over time.

As the concept is elusive, the best advice would be don't ask for the 
object size, find some other solutions to your problem. In some cases, 
it makes sense to ask for object size in some application-specific way, 
and then implement object size methods for specific application classes 
(e.g. structures holding strings would sum up number of characters in 
the strings, etc). Such application-specific way may be inspired by some 
particular (perhaps trivial) serialization format.

I've used object.size() myself only for profiling when quickly 
identifying objects that are probably very large from objects of trivial 
size, where these nuances did not matter, but for that I knew roughly 
what the objects were (e.g. that they were not hiding things in 
environments).

Intuitively, the choices made by object.size() in R are conservative, 
they provide an over-approximation that somewhat intuitively makes sense 
at user level, and they reduce surprises of significant size expansion 
due to minimal updates. The choices and their limitations are 
documented. I think this at least no worse than than say taking into 
account sharing, looking at current "size" of compact objects, etc. One 
could provide more options to object.size(), but I don't think that it 
would be useful.

Best,
Tomas
#
Hi Tomas,
On 3/27/20 07:01, Tomas Kalibera wrote:
They can also provide an "under-approximation" (to say the least) e.g. 
on reference objects where the entire substance of the object is ignored 
which makes object.size() completely meaningless in that case:

   setRefClass("A", fields=c(stuff="ANY"))
   object.size(new("A", stuff=raw(0)))      # 680 bytes
   object.size(new("A", stuff=runif(1e8)))  # 680 bytes

Why wouldn't object.size() look at the content of environments?

Thanks,
H.
#
On 3/27/20 4:39 PM, Herv? Pag?s wrote:
Yes, the treatment of environments is not "over-approximative". It has 
to be bounded somewhere, you can't traverse all captured environments, 
getting to say package namespaces, global environment, code of all 
functions, that would be too over-approximating. For environments used 
as hash maps that contain data, such as in reference classes, it would 
of course be much better to include them, but you can't differentiate 
programmatically. In principle the same environment can be used for both 
things, say a namespace environment can contain data (not clearly 
related to any user-level R object) as well as code. Not mentioning 
things like source references and parse data.

Tomas
#
On Fri, Mar 27, 2020 at 10:39 AM Herv? Pag?s <hpages at fredhutch.org> wrote:

            
As the author, I'm obviously biased, but I do like lobstr::obj_sizes()
which allows you to see the additional size occupied by one object given
any number of other objects. This is particularly important for reference
classes since individual objects appear quite large:

A <- setRefClass("A", fields=c(stuff="ANY"))
lobstr::obj_size(new("A", stuff=raw(0)))
#> 567,056 B

But the vast majority is shared across all instances of that class:

lobstr::obj_size(A)
#> 719,232 B
lobstr::obj_sizes(A, new("A", stuff=raw(0)))
#> * 719,232 B
#> *     720 B
lobstr::obj_sizes(A, new("A", stuff=runif(1e8)))
#> *     719,232 B
#> * 800,000,720 B

Hadley
#
On Fri, Mar 27, 2020 at 11:08 AM Tomas Kalibera <tomas.kalibera at gmail.com>
wrote:
I think the heuristic used in lobstr works well in practice: don't traverse
further than the current environment (supplied as an argument so you can
override), and don't ever traverse past the global or base environments.

Hadley
#
On 3/27/20 12:00, Hadley Wickham wrote:
Nice. Can you clarify the situation with lobstr::obj_size vs 
pryr::object_size? I've heard of the latter before and use it sometimes 
but never heard of the former before seeing Stefan's post. Then I 
checked the authors of both and thought maybe they should talk to each 
other ;-)

Thanks,
H.

  
    
#
On Fri, Mar 27, 2020 at 4:01 PM Herv? Pag?s <hpages at fredhutch.org> wrote:

            
pryr is basically retired :) TBH I don't know why I gave up on it, except
lobstr is a cooler name ? That's where all active development is
happening. (The underlying code is substantially similar although
lobstr includes bug fixes not present in pryr)

Hadley
#
On 3/27/20 15:19, Hadley Wickham wrote:
Good to know, thanks! Couldn't find any mention of pryr being abandoned 
and superseded by lobster (which definitely sounds more yummy) in pryr's 
README.md or DESCRIPTION file. Would be good to put this somewhere.

H.

  
    
#
Thank you Tomas, Herv? and Hadley for the input. Much appreciated!

Stefan

Stefan Schreiber, PhD, P. Biol.
Director EnviroStats Solutions Inc.
Adjunct Professor, Faculty of Agricultural, Life & Environmental
Sciences, University of Alberta
Phone: 780-221-1838
https://envirostats.ca/
On Fri, 27 Mar 2020 at 16:25, Herv? Pag?s <hpages at fredhutch.org> wrote: