# Serialized size

**URL:** <https://discourse.julialang.org/t/serialized-size/19138>\
**Category:** General Usage\
**Created:** [December 30, 2018, 10:05pm UTC](https://discourse.julialang.org/t/serialized-size/19138 "2018-12-30T22:05:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![squaregoldfish](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/squaregoldfish/32/2363_2.png) [@squaregoldfish](https://discourse.julialang.org/u/squaregoldfish)\
**Post date:** [December 30, 2018, 10:05pm UTC](https://discourse.julialang.org/t/serialized-size/19138/1 "2018-12-30T22:05:49Z")

</div>

Is it possible to discover how large something will be when it’s serialized?

I have a data type:  
`NamedTuple{(:x, :y), Tuple{UInt64, UInt64}}`

`sizeof` tells me this is 16 bytes, and the `serialize` documentation says that it will add an 8-byte header - which gives 24 bytes. However, experiments show that the serialized form is actually 34 bytes. So `sizeof` clearly isn’t giving me what I need. Is there something I can use programmatically?

---

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [December 30, 2018, 10:32pm UTC](https://discourse.julialang.org/t/serialized-size/19138/2 "2018-12-30T22:32:35Z")

</div>

Perhaps not the most efficient, but you can create your own IO type like this:

```julia
mutable struct ByteCounterIO <: IO
    count::Int
    ByteCounterIO() = new(0)
end

Base.write(s::ByteCounterIO, x::UInt8) = s.count += 1

io = ByteCounterIO()
t = (x = 34, y = 18)

using Serialization
serialize(io, t)

io.count

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 31, 2018, 10:02am UTC](https://discourse.julialang.org/t/serialized-size/19138/3 "2018-12-31T10:02:45Z")

</div>

Are you constructing a `Serializer` object and writing the header once with `Serialization.writeheader` as suggested in the [docs](https://docs.julialang.org/en/v1/stdlib/Serialization/)? Unless you do this, `Serializer.serialize` will write a header _each time_ you serialize something if you use it on an `<: IO` stream.
