# \`sizeof\` on custom struct does not check fields

**URL:** https://discourse.julialang.org/t/sizeof-on-custom-struct-does-not-check-fields/116311
**Category:** General Usage
**Tags:** struct
**Created:** [June 27, 2024, 2:19pm UTC](https://discourse.julialang.org/t/sizeof-on-custom-struct-does-not-check-fields/116311 "2024-06-27T14:19:33Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Leo\_I](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leo_i/32/27262_2.png) [@Leo\_I](https://discourse.julialang.org/u/Leo_I)
#### Post date: [June 27, 2024, 2:19pm UTC](https://discourse.julialang.org/t/sizeof-on-custom-struct-does-not-check-fields/116311/1 "2024-06-27T14:19:33Z")

</div>

For any Julia variable, I wish to check how much memory it uses just to exist.  
The docstring of `sizeof` states:  
`Size, in bytes, of the canonical binary representation of the given DataType T, if any. Or the size, in bytes, of object obj if it is not a DataType.`  
However:

```julia
using SparseArrays
julia> sizeof(rand(Float64,10,10))
800 # 10*10*8 bytes
julia> sizeof(sprand(Float64,10,10,0.1))
40
julia> sizeof(SparseMatrixCSC{Float64,Int})
40

```

Thus `sizeof` on a struct just returns the size of its type. Wouldn’t it make more sense to have a recursive call

```julia
sizeof(X ::SparseMatrixCSC{Tv,Ti}) where {Ti<:Integer, Tv} = 
    sizeof(SparseMatrixCSC{Tv,Ti}) + sizeof(X.colptr) + sizeof(X.rowval) + sizeof(X.nzval)

```

?

Is this functionality implemented in another function? Also, how would it be possible to capture the size of arrays of pointers (other than manual `sum(sizeof(x) for x in xx)`)?

```julia
julia> sizeof([[1,2,3,4,5],[6,7,8],[9,10]])
24 # should be 80 = (5+3+2)*8

```

---

<div class="post-metadata">

### Author: ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)
#### Post date: [June 27, 2024, 2:25pm UTC](https://discourse.julialang.org/t/sizeof-on-custom-struct-does-not-check-fields/116311/2 "2024-06-27T14:25:19Z")

</div>

Use:

```julia-repl
help?> Base.summarysize
  Base.summarysize(obj; exclude=Union{...}, chargeall=Union{...}) -> Int

  Compute the amount of memory, in bytes, used by all unique objects reachable
  from the argument.

  Keyword Arguments
  ≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡

    • exclude: specifies the types of objects to exclude from the
       traversal.

    • chargeall: specifies the types of objects to always charge the
       size of all of their fields, even if those fields would normally
       be excluded.

  See also sizeof.

  Examples
  ≡≡≡≡≡≡≡≡

  julia> Base.summarysize(1.0)
  8
  
  julia> Base.summarysize(Ref(rand(100)))
  848
  
  julia> sizeof(Ref(rand(100)))
  8

```

---

<div class="post-metadata">

### Author: ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)
#### Post date: [June 27, 2024, 4:20pm UTC](https://discourse.julialang.org/t/sizeof-on-custom-struct-does-not-check-fields/116311/3 "2024-06-27T16:20:25Z")

</div>

Yep it’s `summarysize` you want.

`sizeof` is only useful to most users for bits types and is the same as `summarysize` on these. Other types do not have a defined layout so their `sizeof` is exposing internal implementation details.

---

<div class="post-metadata">

### Author: ![Leo\_I](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leo_i/32/27262_2.png) [@Leo\_I](https://discourse.julialang.org/u/Leo_I)
#### Post date: [June 30, 2024, 6:40pm UTC](https://discourse.julialang.org/t/sizeof-on-custom-struct-does-not-check-fields/116311/4 "2024-06-30T18:40:56Z")

</div>

Why do I get

```julia
julia> Base.summarysize([[1,2,3,4,5],[6,7,8],[9,10]])
264

julia> Base.summarysize(rand(Float64,10,10))
840

julia> Base.summarysize(sprand(Float64,10,10,1.0))
1848

```

instead of 80, 800, 800+800+88 (rowval, nzval, colptr) respectively? Where is this hidden memory consumption?

---

<div class="post-metadata">

### Author: ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)
#### Post date: [June 30, 2024, 6:53pm UTC](https://discourse.julialang.org/t/sizeof-on-custom-struct-does-not-check-fields/116311/5 "2024-06-30T18:53:45Z")

</div>

> [@Leo\_I](#):
>
> Where is this hidden memory consumption?

Part is the array header which contains e.g. the size of the array and the dimensions. You can check the size of an empty array is actually `40`.  
This explains your second example exactly and accounts for most of the memory of the other examples. In case of the sparse array there are 2 more fields in the struct for the size I think.
