Why Base.summarysize show pointers twice?

Suppose I have string “Moscow”:

julia> Moscow = "Moscow"
"Moscow"
julia> sizeof(Moscow)
6
julia> Base.summarysize(Moscow)
14

I suppose, that 14 = (Pointer to Array of Chars (8 bytes)) + (6 data bytes).

However, when I create a struct:

julia> struct City
       name::String
       end
julia> Moscow = City("Moscow")
City("Moscow")
julia> Base.summarysize(Moscow)
22

Now it has 22 bytes summary size.

I don’t understand where these extra bytes come from. Why is there a new pointer to the string instead of the string itself?

In contrast

struct Mountain
    first_ascent_year::Int16
    height::UInt16
end
Everest = Mountain(1953,8848)
Base.summarysize(Everest)

returns 4.

julia> struct Foo
       p1::Ptr
       p2::Ptr
       end

julia> obj = Foo(pointer("123"),pointer("456"))
Foo(Ptr{UInt8} @0x0000018c0f24e190, Ptr{UInt8} @0x0000018c0f24e1c8)

julia> sizeof(obj)
16

julia> Base.summarysize(obj)
32

Why are there additional 8 bytes for each non bits type field?

1 Like

As I understand it, each individually allocated object has a “type tag” that precedes the pointer (and data) itself. It’s a bit of internally metadata that’s a part of the total memory usage (summarysize) but not really a part of the data itself (sizeof).

1 Like