# How to delete a variable from memory in Julia?

**URL:** <https://discourse.julialang.org/t/how-to-delete-a-variable-from-memory-in-julia/115649>\
**Category:** Performance\
**Tags:** memory, memory-allocation\
**Created:** [June 14, 2024, 12:35pm UTC](https://discourse.julialang.org/t/how-to-delete-a-variable-from-memory-in-julia/115649 "2024-06-14T12:35:28Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [June 14, 2024, 3:05pm UTC](https://discourse.julialang.org/t/how-to-delete-a-variable-from-memory-in-julia/115649/7 "2024-06-14T15:05:47Z")

</div>

> [@Uranium238](#):
>
> However, this is causing type conversion problems as `Trm1` has type `Array{Float64,4}` and thus it cannot be assigned to `nothing`. Is there any way to overcome this issue and free up the memory associated with `Trm1` just after it’s work has ended?

Are you certain this is exactly what’s happening? To me (and unless TensorOperations is somehow messing with this), it looks like `Trm1 = nothing` should be legal and the problem would be with `g_vvoo = nothing` because you type-declared it `g_vvoo::Array{Float64,4} = deserialize("g_vvoo.jlbin")`. If you instead did `g_vvoo = deserialize("g_vvoo.jlbin")::Array{Float64,4}`, it should be legal to later write `g_vvoo = nothing`.

If you’re concerned about peak memory usage, why do you load all your data up-front? You should be able to notably reduce your peak memory usage by loading each variable immediately before you use it, doing all your operations with it, and then disposing of it afterward (either by reaching the end of a function or by reassigning it). That actually seems like an easy organization of this into mutliple subfunctions: make each subfunction compute the contribution from a single `g_XXXX`. For example,

```julia
function compute_voov(T2::AbstractArray{<:Any, 4}, g_voov::AbstractArray{<:Any, 4})
    @tensor begin
        Trm[a_1,a_2,i_1,i_2] := - g_voov[a_1,i_3,i_1,a_3] * T2[a_2,a_3,i_3,i_2] # Trm2 term
        Trm[a_1,a_2,i_1,i_2] += 2*g_voov[a_1,i_3,i_1,a_3] * T2[a_2,a_3,i_2,i_3] # Trm5 term
    end
    return Trm
end

# add the voov component
@tensor R2u[a_1,a_2,i_1,i_2] += compute_voov(T2, deserialize("g_voov.jlbin")::Array{Float64, 4})[a_1,a_2,i_1,i_2]

```

By the time this line has completed, both the deserialized variable and the generated `Trm` are no longer accessible and can be GC’d.

You could also make a version that updates `R2u` directly, like

```julia
function add_voov!(R2u::AbstractArray{<:Any, 4}, T2::AbstractArray{<:Any, 4}, g_voov::AbstractArray{<:Any, 4})
    @tensor begin
        R2u[a_1,a_2,i_1,i_2] += - g_voov[a_1,i_3,i_1,a_3] * T2[a_2,a_3,i_3,i_2] # Trm2 term
        R2u[a_1,a_2,i_1,i_2] += 2*g_voov[a_1,i_3,i_1,a_3] * T2[a_2,a_3,i_2,i_3] # Trm5 term
    end
    return R2u
end

```

but I don’t anticipate this making a notable difference to performance.

---

_[View the full topic](https://discourse.julialang.org/t/how-to-delete-a-variable-from-memory-in-julia/115649)._
