Skip to content

should Base.summarysize include alignment? #32881

@sjoelund

Description

@sjoelund

Base.summarysize seems inconsistent and feels like the return value cannot be relied upon. The documentation also says different things than the actual implementation.

It says it will compute the memory used by unique objects.

However, strings are not checked for uniqueness (I would expect it to find references that are identical, not identical content):

julia> a="abc"
"abc"

julia> Base.summarysize([a,a,a])
97

julia> Base.summarysize(["abc","def","ghi"])
97

Then it says it will compute the memory used by objects, and as far as I know memory allocations are aligned so I would expect all sizes to be at least multiples of 4, but the string "a" has a size of 9:

julia> Base.summarysize("a")
9

Then we have the fact that sizeof() is used to calculate sizes of each field, or in arrays. An array with 100 elements that is a union of 3 different types all with sizeof=0 will not consume 0 memory:

julia> struct ABC end

julia> Base.summarysize([ABC(),nothing])
40

julia> Base.summarysize([ABC(),nothing,nothing])
40

Wouldn't it make sense to compute this in a C function where the user cannot overload sizeof operators, etc and actually return the size that the data consumes in memory (including overhead)? The garbage collector should know the sizes of all objects after all. A function like that should give the user a much better hint of how much memory an object consumes.

I tested the code in

Julia Version 1.3.0-alpha.0
Commit 6c11e7c2c4 (2019-07-23 01:46 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Core(TM) i7-6820HQ CPU @ 2.70GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.1 (ORCJIT, skylake)

and

Julia Version 1.1.0
Commit 80516ca202 (2019-01-21 21:24 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Core(TM) i7-6820HQ CPU @ 2.70GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.1 (ORCJIT, skylake)

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs decisionA decision on this change is needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions