Free memory of cupy.ndarray

2,726 views
Skip to first unread message

Yusuke Watanabe

unread,
Nov 24, 2015, 11:06:57 PM11/24/15
to Chainer User Group
Hello.
I would like to know whether there is an explicit way to "free" memory of cupy.ndarray, which is allocated on a GPU.


I tried the following code:

import numpy as np
import cupy

x
= np.random.rand(1000,1000)
cupy
.array(x)
cupy
.array(x)
cupy
.array(x)
cupy
.array(x)
cupy
.array(x)
...

Everytime I execute `cupy.array(x)`, GPU memory usage increases by about 8MB.
(I checked it using nvidia-smi command.)
But if execute `y = cupy.array(x)` many times, GPU memory usage does not increase.
(Maybe, memory is freed by some reference counting mechanism.)

My problem is that, if allocate cuda array on iterator, like:

def get_iterator():
    z
= cupy.array(np.random.rand(1000,1000))
   
for i in range(10):
       
yield z[100*i:100*(i+1)]
   
del z # this del seems not free memory

iterator
= get_iterator()
for d in iterator:
   
print d[0]
# memory usage increases after this loop...



the memory is not freed.
Are there any good way to free z?

Any suggestions would be helpful.
Thanks.


Ryosuke Okuta

unread,
Nov 24, 2015, 11:17:17 PM11/24/15
to Chainer User Group
Hello.

`z[100*i:100*(i+1)]` slice make shallow copy object.

Please try code below.
def get_iterator():
    z 
= cupy.array(np.random.rand(1000,1000))
    
for i in range(10):

        
yield z[100*i:100*(i+1)].copy()  # make deep copy object

    
del z # this del seems not free memory 

iterator 
= get_iterator()
for d in iterator:
    
print d[0]
# memory usage increases after this loop...




2015年11月25日水曜日 13時06分57秒 UTC+9 Yusuke Watanabe:

Yusuke Watanabe

unread,
Nov 24, 2015, 11:29:53 PM11/24/15
to Chainer User Group
Oh, great! The memory is freed after iterator is used.
Thank you!

Here is my understanding:
In the original code, there were some reference to z, because z[100*i:100*(i+1)] is shallow copy.
In the new code, there are no reference to z, so it freed.

2015年11月25日水曜日 13時17分17秒 UTC+9 Ryosuke Okuta:

Ryosuke Okuta

unread,
Nov 24, 2015, 11:39:33 PM11/24/15
to Chainer User Group
Yes, you are right.


2015年11月25日水曜日 13時29分53秒 UTC+9 Yusuke Watanabe:

Igor S

unread,
Nov 29, 2015, 10:00:59 AM11/29/15
to Chainer User Group
What if i do not use iterator? Any other ways to free memory?

>>> x = np.array([img_a.reshape((3,224,224))] * 200)
>>> x.shape
(200, 3, 224, 224)
>>> xp = chainer.cuda.cupy
>>> X = xp.asarray(x, dtype=xp.float32)
>>> del X

This one remains in GPU memory after del, maybe i'm missing something?

Ryosuke Okuta

unread,
Nov 30, 2015, 8:14:20 AM11/30/15
to Chainer User Group
Memory will be freed when it is no longer referenced .
In addition to using `del X`, also it will be freed by overwriting `X = None` and exiting from the local scope.


2015年11月30日月曜日 0時00分59秒 UTC+9 Igor S:
Reply all
Reply to author
Forward
0 new messages