# Listing names and sizes of local variables

**URL:** https://discourse.julialang.org/t/listing-names-and-sizes-of-local-variables/58234
**Category:** New to Julia
**Created:** [March 30, 2021, 2:43pm UTC](https://discourse.julialang.org/t/listing-names-and-sizes-of-local-variables/58234 "2021-03-30T14:43:11Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![tyleransom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tyleransom/32/530_2.png) [@tyleransom](https://discourse.julialang.org/u/tyleransom)
#### Post date: [March 30, 2021, 2:43pm UTC](https://discourse.julialang.org/t/listing-names-and-sizes-of-local-variables/58234/1 "2021-03-30T14:43:11Z")

</div>

I am doing some debugging to try and find a memory leak. I would like to be able to see the size of every local variable inside of a function. `InteractiveUtils.jl` seemed like a promising solution, but, being “interactive,” it will only show the information I want if it’s in the global scope.

Consider the following MWE:

```julia
using Random, InteractiveUtils

Random.seed!(123456)

function inside(z)
    y = z./100
    return y
end

function wrapper()
    x = 1_000 .* rand(1_000)
    w = inside(x)
    @show varinfo()
    return w
end

@show varinfo()
p = wrapper()
@show varinfo()

```

What I would like is to see the sizes of `w` and `x` inside of the wrapper function. But instead I only get the following because of the local scope.

```julia
| name | size | summary |
|:---------------- | -----------:|:--------------- |
| Base | | Module |
| Core | | Module |
| InteractiveUtils | 183.034 KiB | Module |
| Main | | Module |
| inside | 0 bytes | typeof(inside) |
| wrapper | 0 bytes | typeof(wrapper) |

```

If anyone has ideas about how to see sizes of local variables, I would be most grateful. I have consulted [this](https://discourse.julialang.org/t/listing-the-names-of-local-variables/9776) and [this](https://discourse.julialang.org/t/outofmemoryerror-instead-of-allocating-too-much-resources-in-the-job-scheduler/45575/4) Discourse topics already. They have been helpful, but not quite what I am looking for.

---

<div class="post-metadata">

### Author: ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)
#### Post date: [March 30, 2021, 2:56pm UTC](https://discourse.julialang.org/t/listing-names-and-sizes-of-local-variables/58234/2 "2021-03-30T14:56:51Z")

</div>

I am completely naive here, but shouldn’t the size of an array just be a function of its size and element type, and it is independent of scope? Or are your actual types more complicated than arrays?

---

<div class="post-metadata">

### Author: ![tyleransom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tyleransom/32/530_2.png) [@tyleransom](https://discourse.julialang.org/u/tyleransom)
#### Post date: [March 30, 2021, 3:00pm UTC](https://discourse.julialang.org/t/listing-names-and-sizes-of-local-variables/58234/3 "2021-03-30T15:00:07Z")

</div>

You’re correct: I’m just using arrays and DataFrames. Is there a source I could consult that would map “number of elements in a Float64 array” to “number of MB occupying RAM”? I’m no computer scientist. But I would happily write my own function that could do this calculation for me.

---

<div class="post-metadata">

### Author: ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)
#### Post date: [March 30, 2021, 3:10pm UTC](https://discourse.julialang.org/t/listing-names-and-sizes-of-local-variables/58234/4 "2021-03-30T15:10:53Z")

</div>

I was thinking you could just `println(size(w))` or something from inside whatever function you want to peek into, then you can create that variable in the REPL and call `varinfo()` to see how big it is.

Someone might well come along and correct me on this, but I think that a float is 8 bytes. So 100 floats is something like 800 bytes + some overhead,

```julia
x 840 bytes 100-element Vector{Float64}

```

---

<div class="post-metadata">

### Author: ![tyleransom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tyleransom/32/530_2.png) [@tyleransom](https://discourse.julialang.org/u/tyleransom)
#### Post date: [March 31, 2021, 2:12pm UTC](https://discourse.julialang.org/t/listing-names-and-sizes-of-local-variables/58234/5 "2021-03-31T14:12:28Z")

</div>

Here’s an even better solution: `Base.summarysize()`! Modifying the MWE from above:

```julia
using Random, InteractiveUtils

Random.seed!(123456)

function inside(z)
    y = z./100
    return y
end

function wrapper()
    x = 1_000 .* rand(1_000)
    w = inside(x)
    @show Base.summarysize(x)/1024
    @show Base.summarysize(w)/1024
    return w
end

p = wrapper()
@show varinfo()

```

which returns

```julia
Base.summarysize(x) / 1024 = 7.8515625
Base.summarysize(w) / 1024 = 7.8515625
varinfo() = | name | size | summary |
|:---------------- | -----------:|:----------------------------- |
| Base | | Module |
| Core | | Module |
| InteractiveUtils | 182.987 KiB | Module |
| Main | | Module |
| inside | 0 bytes | typeof(inside) |
| p | 7.852 KiB | 1000-element Array{Float64,1} |
| wrapper | 0 bytes | typeof(wrapper) |

```
