# Is sizeof() giving incorrect results instead of errors for structs with mutable fields?

**URL:** https://discourse.julialang.org/t/is-sizeof-giving-incorrect-results-instead-of-errors-for-structs-with-mutable-fields/104721
**Category:** New to Julia
**Created:** [October 8, 2023, 2:30am UTC](https://discourse.julialang.org/t/is-sizeof-giving-incorrect-results-instead-of-errors-for-structs-with-mutable-fields/104721 "2023-10-08T02:30:44Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![sadish-d](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sadish-d/32/48058_2.png) [@sadish-d](https://discourse.julialang.org/u/sadish-d)
#### Post date: [October 8, 2023, 2:30am UTC](https://discourse.julialang.org/t/is-sizeof-giving-incorrect-results-instead-of-errors-for-structs-with-mutable-fields/104721/1 "2023-10-08T02:30:44Z")

</div>

Example:

```julia
struct IMMUT
    a::Int64
    b::Bool
end

sizeof(IMMUT(0, false)) == 16 # true since this is "with padding", as the documentation describes.

struct MUT
    a::Array
end

vctr = Int64[1, 2, 3]

sizeof(vctr) == 24 # true
sizeof(MUT(vctr)) == 24 # false
sizeof(MUT(vctr)) == 8 # true

```

---

<div class="post-metadata">

### Author: ![torrance](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torrance/32/38990_2.png) [@torrance](https://discourse.julialang.org/u/torrance)
#### Post date: [October 8, 2023, 2:48am UTC](https://discourse.julialang.org/t/is-sizeof-giving-incorrect-results-instead-of-errors-for-structs-with-mutable-fields/104721/2 "2023-10-08T02:48:58Z")

</div>

In this case, I believe `sizeof` is returning the size of a pointer to the array, which on a 64 bit system is 8 bytes.

Since the array is heap allocated and tracked by the GC, it cannot be stored inline in the struct. The struct therefore contains a reference that points to the healp allocated object. It is for this same reason that `isbits` will return false on the second struct.

---

<div class="post-metadata">

### Author: ![sadish-d](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sadish-d/32/48058_2.png) [@sadish-d](https://discourse.julialang.org/u/sadish-d)
#### Post date: [October 8, 2023, 2:57am UTC](https://discourse.julialang.org/t/is-sizeof-giving-incorrect-results-instead-of-errors-for-structs-with-mutable-fields/104721/3 "2023-10-08T02:57:49Z")

</div>

> In this case, I believe `sizeof` is returning the size of a pointer to the array, which on a 64 bit system is 8 bytes.

This makes sense.

Is this the expected/correct behavior for `sizeof()` though?

I’m using Julia 1.9.0, and this is the documentaiton:

```julia
sizeof(T::DataType)
  sizeof(obj)

  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.

  See also Base.summarysize.

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> sizeof(Float32)
  4
  
  julia> sizeof(ComplexF64)
  16
  
  julia> sizeof(1.0)
  8
  
  julia> sizeof(collect(1.0:10.0))
  80
  
  julia> struct StructWithPadding
             x::Int64
             flag::Bool
         end
  
  julia> sizeof(StructWithPadding) # not the sum of `sizeof` of fields due to padding
  16
  
  julia> sizeof(Int64) + sizeof(Bool) # different from above
  9

  If DataType T does not have a specific size, an error is thrown.

  julia> sizeof(AbstractArray)
  ERROR: Abstract type AbstractArray does not have a definite size.
  Stacktrace:
  [...]

  ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

  sizeof(str::AbstractString)

  Size, in bytes, of the string str. Equal to the number of code units in str multiplied by the size, in bytes, of one code unit in str.

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> sizeof("")
  0
  
  julia> sizeof("∀")
  3

```

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [October 8, 2023, 3:59am UTC](https://discourse.julialang.org/t/is-sizeof-giving-incorrect-results-instead-of-errors-for-structs-with-mutable-fields/104721/4 "2023-10-08T03:59:58Z")

</div>

> [@sadish-d](#):
>
> Is this the expected/correct behavior for `sizeof()` though?

Yes. Maybe `Base.summarysize` is closer to what you want, though I don’t think that is implemented to count all relevant memory either.

```julia
julia> Base.summarysize(Int64[])
40

julia> Base.summarysize(vctr) # +24 for 3 elements
64

julia> Base.summarysize(MUT(vctr)) # +8 for 1 pointer
72

```
