Any way to delete an object and free memory?

Hi,

I have a Monte Carlo simulation code that uses instances of a mutable struct that contain arrays. These arrays can be really large when the number of particles in the simulation increase. In the code, I have one instance (call it W1) of that struct, and at the end of an iteration I need to create a new instance (call it W2) where I have to copy only one array from W1. But prior to copying, I have to initialize to zero all arrays in W2.
Trouble is that I run out of GPU memory when initializing (filling with 0.f0) W2 WHILE W1 is still there. So my question is this: is there a way to explicitly kill W1, freeing GPU memory, before creating W2? If that was the case, I could simply copy from W1 the array I need to copy to W2, then delete W1, Initialize W2, copy the stored array, and go…

Thanks a lot,

Ferran.

Julia’s GC will collect objects when they are unreachable, so if you assign nothing it should be able to collect.

1 Like

Thanks, I’ll try that.
BTW is there a simple way to monitor the amount of GPU memory used in the code?
Best,
Ferran.

Uhmm… works partially. I don’t know when the GC does its job, but it does not do it when I need it.
Is there a way to force it? I mean, after the W1=nothing, can I add something to force GC?
Thanks,
Ferran.

You can call GC.gc(), with the argument true to collect more. But you shouldn’t need to, as right before running out of memory, CUDA.jl should do this for you. Besides, ‘freed’ memory isn’t returned to CUDA, so it might still look like your GPU is full while that memory is just cached. You can call CUDA.reclaim() to reclaim all that memory, but this generally shouldn’t be required (unless, say, you’re working with an external library that doesn’t use CUDA.jl’s memory pool).

You can monitor different levels of memory usage. Easiest:

julia> CUDA.memory_status()
Effective GPU memory usage: 12.61% (1.985 GiB/15.744 GiB)
CUDA allocator usage: 4.000 MiB
binned usage: 4.000 MiB (4.000 MiB allocated, 0 bytes cached)

Why do you need to create a W2 at all when you have a mutable W1 and reuse parts of it anyways? Wouldn’t it be easier to zero out the parts of W1 that you don’t need and continue to use that one?