# Clear memory after using plot!(...)

**URL:** https://discourse.julialang.org/t/clear-memory-after-using-plot/121690
**Category:** New to Julia
**Tags:** plots
**Created:** [October 24, 2024, 10:43am UTC](https://discourse.julialang.org/t/clear-memory-after-using-plot/121690 "2024-10-24T10:43:59Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![EatMyShorts](https://avatars.discourse-cdn.com/v4/letter/e/e5b9ba/32.png) [@EatMyShorts](https://discourse.julialang.org/u/EatMyShorts)
#### Post date: [October 24, 2024, 10:43am UTC](https://discourse.julialang.org/t/clear-memory-after-using-plot/121690/1 "2024-10-24T10:43:59Z")

</div>

Hello,

is there a simple command for Julia to clear memory after using plot!(…) without having to constantly restart VS Code?

Best regards …

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [October 24, 2024, 11:21am UTC](https://discourse.julialang.org/t/clear-memory-after-using-plot/121690/2 "2024-10-24T11:21:32Z")

</div>

Why would you want that? Can you explain your issue?

---

<div class="post-metadata">

### Author: ![EatMyShorts](https://avatars.discourse-cdn.com/v4/letter/e/e5b9ba/32.png) [@EatMyShorts](https://discourse.julialang.org/u/EatMyShorts)
#### Post date: [October 24, 2024, 11:27am UTC](https://discourse.julialang.org/t/clear-memory-after-using-plot/121690/3 "2024-10-24T11:27:23Z")

</div>

… test environment … I create new plots via the console and no longer need the previous plots.  
For this reason, I want to clear the memory so that I can create new plots again with plots!(…)

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [October 24, 2024, 11:36am UTC](https://discourse.julialang.org/t/clear-memory-after-using-plot/121690/4 "2024-10-24T11:36:56Z")

</div>

Do you want to delete the series inside the plot canvas and keep the decoration (title, axes, axes limits and labels), to be able to replot onto the same canvas ?

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [October 24, 2024, 11:38am UTC](https://discourse.julialang.org/t/clear-memory-after-using-plot/121690/5 "2024-10-24T11:38:57Z")

</div>

You can always create new plots, by either starting another plot with, for example:

```julia
plot(rand(10))
plot!(rand(10))
plot(rand(20)) # new plot
plot!(rand(20))

```

or, which might give you better control, assing the plot to a variable:

```julia
p1 = plot(rand(10))
plot!(p1, rand(10)))

p2 = plot(rand(20))
plot!(p2, rand(20)))

```

If you have a **memory** issue (which is unlikely, I guess), you can try setting the variables to `nothing`, so they become-garbage collected at some point:

```julia
p1 = nothing

```

---

<div class="post-metadata">

### Author: ![EatMyShorts](https://avatars.discourse-cdn.com/v4/letter/e/e5b9ba/32.png) [@EatMyShorts](https://discourse.julialang.org/u/EatMyShorts)
#### Post date: [October 24, 2024, 12:01pm UTC](https://discourse.julialang.org/t/clear-memory-after-using-plot/121690/6 "2024-10-24T12:01:34Z")

</div>

I just want use the console and I want clear the memory for the plots. I don’t want fill new variables.  
I have an vector array from numerical simulations.  
I just plot some vectors from this array.  
Afterwards, I change something in the simulation and I want plot vectors from the new array again.  
I just use the console for plot!(…).  
It´s for me faster, than write any blocks in the main script.  
For this issue I restart VS Code again, again and again.

@rafael.guerra: I want clear all points from the previous plots.

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [October 24, 2024, 12:12pm UTC](https://discourse.julialang.org/t/clear-memory-after-using-plot/121690/7 "2024-10-24T12:12:57Z")

</div>

Try this code, as example:

```julia
using Plots
p = plot(sin, lims=(-2,2), title="My playing canvas")
plot!(p, cos)

# Clear plots series
foreach(sp -> empty!(sp.series_list), p.subplots)
p

# replot while keeping plot limits and decorations
plot!(p, cos)

```

---

<div class="post-metadata">

### Author: ![EatMyShorts](https://avatars.discourse-cdn.com/v4/letter/e/e5b9ba/32.png) [@EatMyShorts](https://discourse.julialang.org/u/EatMyShorts)
#### Post date: [October 24, 2024, 12:16pm UTC](https://discourse.julialang.org/t/clear-memory-after-using-plot/121690/8 "2024-10-24T12:16:56Z")

</div>

hmmm … Is there really no way to reset the memory entry like I would restart VS Code? … like a kill switch ^^  
Okay … I will try it.

---

<div class="post-metadata">

### Author: ![tomaklutfu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomaklutfu/32/2411_2.png) [@tomaklutfu](https://discourse.julialang.org/u/tomaklutfu)
#### Post date: [October 24, 2024, 12:49pm UTC](https://discourse.julialang.org/t/clear-memory-after-using-plot/121690/9 "2024-10-24T12:49:39Z")

</div>

The julia language is garbage collected(GC). If you close the plot and loose reference to the plot, the language itself de-allocates the memory. If you need to manually trigger memory garbage collection for some reason, you can trigger it by `GC.gc()` but first you need to re-associate(`=`) bindings(variables) which reference memory to something like `nothing`. You usually do not need to manually clear memory due to the GC and memory will be reused if the plot redrawn with something new. If you are asking for plot pane in VS Code it is the julia extensions feature to show you older plots in the same session.

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [October 24, 2024, 2:55pm UTC](https://discourse.julialang.org/t/clear-memory-after-using-plot/121690/10 "2024-10-24T14:55:55Z")

</div>

> [@EatMyShorts](#):
>
> I want clear all points from the previous plots.

For that use `plot()` (without the `!`), and you will start a new plot.

---

<div class="post-metadata">

### Author: ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)
#### Post date: [October 24, 2024, 5:01pm UTC](https://discourse.julialang.org/t/clear-memory-after-using-plot/121690/11 "2024-10-24T17:01:31Z")

</div>

I think they talk about the plotpane:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/1/7/170bd87c87bc25f489e37fc40875b5f54c431616.png)  
I also noticed that those actually take up ram, and I dont know how to clear it.

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [October 24, 2024, 5:10pm UTC](https://discourse.julialang.org/t/clear-memory-after-using-plot/121690/12 "2024-10-24T17:10:50Z")

</div>

Uhm… are you sure? I have VSCode sections literally running for weeks with currently \>100 plots in the pane. It doesn’t seem to make any difference in memory usage.

---

<div class="post-metadata">

### Author: ![EatMyShorts](https://avatars.discourse-cdn.com/v4/letter/e/e5b9ba/32.png) [@EatMyShorts](https://discourse.julialang.org/u/EatMyShorts)
#### Post date: [October 24, 2024, 7:21pm UTC](https://discourse.julialang.org/t/clear-memory-after-using-plot/121690/13 "2024-10-24T19:21:11Z")

</div>

I am running simulations with different initial data.  
I get a solution vector-matrix; after each time-step the programm saves a solution vector in this matrix. I compare some solutions from different time-steps directly with  
plot!(solution[1])  
plot!(solution[10])

Nothing special … all graphs are included in a plot.

After changing the initial data for a new simulation run, I want also plot the new solutions in a new plot.  
But Julia plots the new data in the same plot, which includes the old data.

So I am looking just for a console command like plot!(…), to clear the previous data from the plot (or initial a new plot).

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [October 24, 2024, 7:31pm UTC](https://discourse.julialang.org/t/clear-memory-after-using-plot/121690/14 "2024-10-24T19:31:34Z")

</div>

Did you try `plot(...)` (without ! )?

---

<div class="post-metadata">

### Author: ![EatMyShorts](https://avatars.discourse-cdn.com/v4/letter/e/e5b9ba/32.png) [@EatMyShorts](https://discourse.julialang.org/u/EatMyShorts)
#### Post date: [October 24, 2024, 7:42pm UTC](https://discourse.julialang.org/t/clear-memory-after-using-plot/121690/15 "2024-10-24T19:42:13Z")

</div>

I cannot compare solution vectors in a same plot just with plot(…).  
I get a new plot for each vector.

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [October 24, 2024, 7:45pm UTC](https://discourse.julialang.org/t/clear-memory-after-using-plot/121690/16 "2024-10-24T19:45:09Z")

</div>

So just do:

```julia
plot(vec1)
plot!(vec2, vec3...)

```

You can also define your own vector plot function that combines these two calls, like:

```julia
function vecplot(vec)
    plot(vec[1])
    plot!(vec[2:end])
end

```

I did not try this myself, just as idea.

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [October 24, 2024, 7:48pm UTC](https://discourse.julialang.org/t/clear-memory-after-using-plot/121690/17 "2024-10-24T19:48:32Z")

</div>

> [@EatMyShorts](#):
>
> I get a new plot for each vector.

The `plot()` without the `!` creates a new plot, and the `plot!()` with the `!` modifies that previously created plot.

That is, in each new comparison, the first plot is without the `!`.

---

<div class="post-metadata">

### Author: ![EatMyShorts](https://avatars.discourse-cdn.com/v4/letter/e/e5b9ba/32.png) [@EatMyShorts](https://discourse.julialang.org/u/EatMyShorts)
#### Post date: [October 24, 2024, 7:49pm UTC](https://discourse.julialang.org/t/clear-memory-after-using-plot/121690/18 "2024-10-24T19:49:51Z")

</div>

Let’s take a look at this example…

 ![170bd87c87bc25f489e37fc40875b5f54c431616_2_948x748](https://global.discourse-cdn.com/julialang/original/3X/7/6/762117cddac3500d9b29fb42535b350d5027f0e9.png)

Is there a way to clear the counter from ‘Julia Plots (5/5)’ to zero?

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [October 24, 2024, 7:51pm UTC](https://discourse.julialang.org/t/clear-memory-after-using-plot/121690/19 "2024-10-24T19:51:24Z")

</div>

Restart VSCode. If you don’t like that, create an issue at: [GitHub · Where software is built](https://github.com/julia-vscode/julia-vscode/issues)

My comment: Using advanced features of VSCode creates more problems than it solves.

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [October 24, 2024, 7:53pm UTC](https://discourse.julialang.org/t/clear-memory-after-using-plot/121690/20 "2024-10-24T19:53:29Z")

</div>

> [@EatMyShorts](#):
>
> Is there a way to clear the counter from ‘Julia Plots (5/5)’ to zero?

You are mixing two things. That has nothing to do with the subsequent plots being added to the same plot or no.

Isn’t just:

```julia
plot(solution[1]) # no !
plot!(solution[2]) # with !

```

what you want?

If not, I think you need to explain better what’s your problem.

[Next page](https://discourse.julialang.org/t/clear-memory-after-using-plot/121690.md?page=2)
