# Unable to serialize SharedArrays

**URL:** <https://discourse.julialang.org/t/unable-to-serialize-sharedarrays/13646>\
**Category:** General Usage\
**Created:** [August 18, 2018, 1:13am UTC](https://discourse.julialang.org/t/unable-to-serialize-sharedarrays/13646 "2018-08-18T01:13:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)\
**Post date:** [August 18, 2018, 1:13am UTC](https://discourse.julialang.org/t/unable-to-serialize-sharedarrays/13646/1 "2018-08-18T01:13:45Z")

</div>

I use Serialization.serialize() function to write julia objects on disk since I found it more efficient than other formats (like JLD2) and it’s for short term shortage so I can use the the same Julia runtime to read it back.

It works well except that SharedArray cannot be serialized properly. It seems that it may be just saving the pointers so the output file is very small even though I have a large array. Is there any to do this properly? I prefer not to translate it to a regular Array because it’s already large and I don’t have enough memory…

```julia
julia> using SharedArrays, Serialization

julia> A = SharedArray{Float64,2}((2,1000000));

julia> open("A.jo", "w") do file serialize(file, A) end

julia> stat("A.jo")
StatStruct(mode=0o100666, size=181)

```

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [August 18, 2018, 3:46am UTC](https://discourse.julialang.org/t/unable-to-serialize-sharedarrays/13646/2 "2018-08-18T03:46:45Z")

</div>

Can you `reinterpret` a `SharedArray` and then serialize that?

---

<div class="post-metadata">

**Author:** ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)\
**Post date:** [August 18, 2018, 5:52am UTC](https://discourse.julialang.org/t/unable-to-serialize-sharedarrays/13646/3 "2018-08-18T05:52:46Z")

</div>

I guess it makes sense to only pass pointers rather than real data when sending a SharedArray from one process to another (which is the real reason for serialization in the first place) but it doesn’t work in my case.

Reinterpreting seems to allocate:

```julia
julia> typeof(A)
SharedArray{Float64,2}

julia> size(A)
(2, 1000000)

julia> Base.summarysize(A)
16000365

julia> f(A) = collect(reinterpret(Float64, A))
f (generic function with 1 method)

julia> @allocated f(A)
16185831

```

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [August 18, 2018, 5:58am UTC](https://discourse.julialang.org/t/unable-to-serialize-sharedarrays/13646/4 "2018-08-18T05:58:07Z")

</div>

> [@tk3369](#):
>
> Reinterpreting seems to allocate:

See [Big overhead with the new lazy reshape/reinterpret - #32 by pablosanjose](https://discourse.julialang.org/t/big-overhead-with-the-new-lazy-reshape-reinterpret/7635/32)
