# Polymorphic Type Instance Memory Overhead

**URL:** https://discourse.julialang.org/t/polymorphic-type-instance-memory-overhead/14211
**Category:** General Usage
**Created:** [August 28, 2018, 8:32pm UTC](https://discourse.julialang.org/t/polymorphic-type-instance-memory-overhead/14211 "2018-08-28T20:32:44Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![nordlow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nordlow/32/4970_2.png) [@nordlow](https://discourse.julialang.org/u/nordlow)
#### Post date: [August 28, 2018, 8:32pm UTC](https://discourse.julialang.org/t/polymorphic-type-instance-memory-overhead/14211/1 "2018-08-28T20:32:45Z")

</div>

How do I measure the memory overhead of an instance of a polymorphic type in Julia?

I am particularly interested in

- union types
- sub-types of `Any`

Further, does Julia have some memory-usage optimization tricks for the descriminator in union-types? For instance, if all types in the union are of similar size and share a set of bit-patterns that are all unused by all the types, these bit-patterns can be used as a descriminator.

An example would be a `Union{}` of different kinds of pointers all with alignment of 16, which in this case, would leave the 3 least significant bits unused. Assuming the GC is aware of this.

BTW: Is there any documentation on the internals of the current GC in Julia?

---

<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: [August 29, 2018, 8:49am UTC](https://discourse.julialang.org/t/polymorphic-type-instance-memory-overhead/14211/2 "2018-08-29T08:49:34Z")

</div>

> [@nordlow](#):
>
> memory overhead of an instance of a polymorphic type

I thought instances were always concrete.

---

<div class="post-metadata">

### Author: ![nordlow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nordlow/32/4970_2.png) [@nordlow](https://discourse.julialang.org/u/nordlow)
#### Post date: [August 29, 2018, 9:16am UTC](https://discourse.julialang.org/t/polymorphic-type-instance-memory-overhead/14211/3 "2018-08-29T09:16:26Z")

</div>

Ok, I meant an instance of a type that inherits (implements) `Any`.

But it turns out that

```julia
struct X <: Any end
sizeof(X)

```

returns 0 because Julia’s type model is not what I thought it was. Is union types or composition of inheritance the way to express traditional object hierarchies in Julia?

Julia’s type system is new to me. I’m used to thinking in C++, Rust, Python and D’s type systems. Is Julia’s anywhere similar to Rust’s multi-trait-system?

---

<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: [August 29, 2018, 9:22am UTC](https://discourse.julialang.org/t/polymorphic-type-instance-memory-overhead/14211/4 "2018-08-29T09:22:11Z")

</div>

> [@nordlow](#):
>
> Is union types or composition of inheritance the way to express traditional object hierarchies in Julia?

I don’t know what a “traditional object hierarchy” is. Julia has single-ancestor subtypes, where the leafs are concrete and the rest is abstract types. These are mostly used for governing dispatch. Composition is recommended for what some OO languages solve with inheritance. See

> [@Composition and inheritance: the Julian way](https://discourse.julialang.org/t/composition-and-inheritance-the-julian-way/11231):
>
> I am a relatively newbie and I am wondering what is the most appropriate Julian way to reuse the code. I don’t want to trigger discussions on topics already discussed a million times. This post simply aims to lay down a clear list of rules to be followed to write good Julia code, and pose a couple of questions. Let’s start with an example: I have a type Person mutable struct Person \<: AbstractPerson name::String age::Int end and a lot (hundreds) of methods dealing with it. Now I wa…

> [@Workaround for traditional inheritance features in object-oriented languages](https://discourse.julialang.org/t/workaround-for-traditional-inheritance-features-in-object-oriented-languages/1195/):
>
> I am using the Julia type system seriously for the first time and have just learned that it doesn’t support inheritance from non-abstract types nor multiple inheritance. This statement applies to Julia v0.5. Could you please explain how one can workaround these limitations, specifically: How you develop code with inheritance from abstract types only? Does it cover all possible use cases? Is there any workaround for the lack of multiple inheritance?

> [@OOP in Julia, inherit from parametric composite type](https://discourse.julialang.org/t/oop-in-julia-inherit-from-parametric-composite-type/1841):
>
> Hey, so this might be a rather vague question but anyway here it is. I am really having a hard time getting to grasp with OOP in Julia. I am developing a package for specific clinical trial designs and these designs can essentially be represented as a tuple of vectors. Trying to be thorough I implement them as parametric composite types. E.g. the base design could be type DesignA{T1, T2} v1::Vector{T1} v2::Vector{T2} end Now another design could be just like DesignA but with additional pa…

and similar topics.

Also, I would not worry about representation in memory etc, other than what is described in the

[https://docs.julialang.org/en/stable/manual/performance-tips/](https://docs.julialang.org/en/stable/manual/performance-tips/)

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [August 29, 2018, 9:39am UTC](https://discourse.julialang.org/t/polymorphic-type-instance-memory-overhead/14211/5 "2018-08-29T09:39:56Z")

</div>

Adding on to that, if more details are wanted:

[https://docs.julialang.org/en/stable/devdocs/types/](https://docs.julialang.org/en/stable/devdocs/types/)

[https://docs.julialang.org/en/stable/devdocs/object/](https://docs.julialang.org/en/stable/devdocs/object/)

But this really shouldn’t be needed during normal development.
