# What is the memory overhead of a mutable struct?

**URL:** <https://discourse.julialang.org/t/what-is-the-memory-overhead-of-a-mutable-struct/26963>\
**Category:** Performance\
**Created:** [July 30, 2019, 3:25am UTC](https://discourse.julialang.org/t/what-is-the-memory-overhead-of-a-mutable-struct/26963 "2019-07-30T03:25:46Z")\
**Posts on this page:** 1\
**Showing post:** 8

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [July 30, 2019, 6:16pm UTC](https://discourse.julialang.org/t/what-is-the-memory-overhead-of-a-mutable-struct/26963/8 "2019-07-30T18:16:10Z")

</div>

The issue is the call `setindex!(testers, MemTester(i), i)`, i.e. `testers[i] = MemTester(i)`. The first argument is of unknown type (because it is a global variable), therefore julia needs to call into the runtime: It calls a C function that walks the method table and figures out whom to call.

I think the issue is that this C function expects arguments that are pointers to valid heap-allocated julia objects and then extracts the argument types from their headers. We already have a pointer to `MemTester(i)`, but we also need a pointer to an object that contains the integer `i` and an object header that tells the runtime that it is an integer. We obtain this object via `jl_box_int64`. That appears to allocate sometimes.

TLDR: Don’t use non-const globals. Really, don’t.

---

_[View the full topic](https://discourse.julialang.org/t/what-is-the-memory-overhead-of-a-mutable-struct/26963)._
