# Memory allocation problem

**URL:** <https://discourse.julialang.org/t/memory-allocation-problem/11352>\
**Category:** Performance\
**Created:** [June 1, 2018, 9:36pm UTC](https://discourse.julialang.org/t/memory-allocation-problem/11352 "2018-06-01T21:36:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jinliangwei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jinliangwei/32/1929_2.png) [@jinliangwei](https://discourse.julialang.org/u/jinliangwei)\
**Post date:** [June 1, 2018, 9:36pm UTC](https://discourse.julialang.org/t/memory-allocation-problem/11352/1 "2018-06-01T21:36:17Z")

</div>

I profiled by my program by tracking user memory allocation. The program seems to incur memory allocation at places I don’t expect.

My program is pretty long so I have it updated to Github: [https://github.com/jinliangwei/julia\_mem/blob/master/serial\_lda.jl](https://github.com/jinliangwei/julia_mem/blob/master/serial_lda.jl)

There is also the memory tracking output and a sample dataset.

The function that I am trying to optimize is `sample_one_word` which is currently the bottleneck of my program.

Mainly I don’t understand why memory allocations happen when the program reads and writes a single element of a `Vector`, for example, see [line 178](https://github.com/jinliangwei/julia_mem/blob/master/serial_lda.jl.mem#L178), [line 272](https://github.com/jinliangwei/julia_mem/blob/master/serial_lda.jl.mem#L272), [line 278](https://github.com/jinliangwei/julia_mem/blob/master/serial_lda.jl.mem#L278), etc.

This program is about 2~3X slower than a C++ implementation and I am suspecting memory allocation is one main bottleneck.

---

<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:** [June 1, 2018, 10:02pm UTC](https://discourse.julialang.org/t/memory-allocation-problem/11352/2 "2018-06-01T22:02:40Z")

</div>

This naively looks like a type stability issue. Can you update your code to generate random test-data first, so that we can copy-paste it into the REPL?

---

<div class="post-metadata">

**Author:** ![favba](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/favba/32/2735_2.png) [@favba](https://discourse.julialang.org/u/favba)\
**Post date:** [June 1, 2018, 10:06pm UTC](https://discourse.julialang.org/t/memory-allocation-problem/11352/3 "2018-06-01T22:06:48Z")

</div>

Have you precompiled your code first? That line 178 seems like compilation allocation. Note that the allocation printed some times is off a line…

---

<div class="post-metadata">

**Author:** ![jinliangwei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jinliangwei/32/1929_2.png) [@jinliangwei](https://discourse.julialang.org/u/jinliangwei)\
**Post date:** [June 1, 2018, 10:13pm UTC](https://discourse.julialang.org/t/memory-allocation-problem/11352/4 "2018-06-01T22:13:09Z")

</div>

Thanks! I’ve added a program that generates random input: [https://github.com/jinliangwei/julia\_mem/blob/master/serial\_lda\_random.jl](https://github.com/jinliangwei/julia_mem/blob/master/serial_lda_random.jl)

---

<div class="post-metadata">

**Author:** ![jinliangwei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jinliangwei/32/1929_2.png) [@jinliangwei](https://discourse.julialang.org/u/jinliangwei)\
**Post date:** [June 1, 2018, 10:15pm UTC](https://discourse.julialang.org/t/memory-allocation-problem/11352/5 "2018-06-01T22:15:58Z")

</div>

Thanks for your answer!

No, I didn’t precompile my code. The `sample_one_word` function is executed hundreds of thousands of times, but it’s JIT compiled just one. It would be pretty shocking that compiling one function allocates 480MB of memory, right?

---

<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:** [June 1, 2018, 10:27pm UTC](https://discourse.julialang.org/t/memory-allocation-problem/11352/6 "2018-06-01T22:27:22Z")

</div>

Looking at `@code_warntype`, I see that `new_topic` in `sample_all_words` is not inferred and `sample_one_word` has an `Int32`/`Int64` instability.

---

<div class="post-metadata">

**Author:** ![jinliangwei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jinliangwei/32/1929_2.png) [@jinliangwei](https://discourse.julialang.org/u/jinliangwei)\
**Post date:** [June 2, 2018, 7:43pm UTC](https://discourse.julialang.org/t/memory-allocation-problem/11352/7 "2018-06-02T19:43:42Z")

</div>

Thank you! This is very helpful!
