# Understanding when GC.gc() frees memory and doesn't

**URL:** https://discourse.julialang.org/t/understanding-when-gc-gc-frees-memory-and-doesnt/51682
**Category:** General Usage
**Created:** [December 11, 2020, 7:53pm UTC](https://discourse.julialang.org/t/understanding-when-gc-gc-frees-memory-and-doesnt/51682 "2020-12-11T19:53:57Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![tbenst](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbenst/32/3029_2.png) [@tbenst](https://discourse.julialang.org/u/tbenst)
#### Post date: [December 11, 2020, 7:53pm UTC](https://discourse.julialang.org/t/understanding-when-gc-gc-frees-memory-and-doesnt/51682/1 "2020-12-11T19:53:57Z")

</div>

Consider the two code chunks below. When run in vscode, the first fails to free memory as reported by `htop`, while the second frees memory successfully. This is not just a reporting issue–if I create another large array in cell one, I can crash my computer by depleting free memory, while this does not happen in the case of two cells. Any ideas what is going on?

For a script, I cannot follow the two-cell strategy, so how can I force garbage collection?

```julia
## does not free memory
tseries = zeros(1024,1024,5,2000)
tseries = nothing
GC.gc()
sleep(5)
GC.gc()
tseries = zeros(1024,1024,5,2000) # crashes computer

```

```julia
## Two cells queued sequentially (no delay): does free memory
tseries = zeros(1024,1024,5,2000)
tseries = nothing
GC.gc()
##
GC.gc()
tseries = zeros(1024,1024,5,2000) # works fine

```

---

<div class="post-metadata">

### Author: ![paulmelis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paulmelis/32/35063_2.png) [@paulmelis](https://discourse.julialang.org/u/paulmelis)
#### Post date: [December 14, 2020, 8:11am UTC](https://discourse.julialang.org/t/understanding-when-gc-gc-frees-memory-and-doesnt/51682/2 "2020-12-14T08:11:30Z")

</div>

Did you try the first version (single cell) from the Julia REPL, i.e. outside of VS? For me, with 1.5.3, the first `GC.gc()` call definitely reclaims the unused array, looking at the memory usage of my system.
