# Understanding memory usage of a kernel

**URL:** <https://numba.discourse.group/t/understanding-memory-usage-of-a-kernel/2790>\
**Category:** Support: How do I do ...?\
**Created:** [November 11, 2024, 1:14am UTC](https://numba.discourse.group/t/understanding-memory-usage-of-a-kernel/2790 "2024-11-11T01:14:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![marcelosena](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/marcelosena/32/1152_2.png) [@marcelosena](https://numba.discourse.group/u/marcelosena)\
**Post date:** [November 11, 2024, 1:14am UTC](https://numba.discourse.group/t/understanding-memory-usage-of-a-kernel/2790/1 "2024-11-11T01:14:47Z")

</div>

Hi

I am trying to understand the measurement of memory usage in Numba.

I am allocating here an array with 10^9 float 32 numbers, which I expect to allocate 4gb of memory.

However, when trying to measure the allocation, i get an apparenty much smaller allocation.

MemoryInfo(free=6140526592, total=6241124352)

Here is the code I use:

```auto
from numba import cuda
import numpy as np

@cuda.jit
def cuda_run(arr):
	thread = cuda.grid(1)
	arr[thread] = 1.0

tot_calc = 10**9
arr = np.zeros(tot_calc, dtype = 'float32')

print(cuda.select_device(0))

threadsperblock = 128
blockspergrid = math.ceil(tot_calc / threadsperblock)
start = time.time()
cuda_run[blockspergrid, threadsperblock](arr)
end = time.time()

print(cuda.current_context().get_memory_info())

del cuda_run
print(cuda.current_context().get_memory_info())

cuda.current_context().reset()
print(cuda.current_context().get_memory_info())

cuda.driver.driver.reset()
print(cuda.current_context().get_memory_info())

```

Is this the expected behavior? Is my memory calculation wrong? Or the way I calculate it?

Thanks and any help is appreciated!

---

<div class="post-metadata">

**Author:** ![marcelosena](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/marcelosena/32/1152_2.png) [@marcelosena](https://numba.discourse.group/u/marcelosena)\
**Post date:** [November 11, 2024, 9:29pm UTC](https://numba.discourse.group/t/understanding-memory-usage-of-a-kernel/2790/2 "2024-11-11T21:29:01Z")

</div>

Ok, I think I managed to understand this myself. Apparently Numba in the background does all the necessary cleanup of memory, so the numbers before do not actually reflect proper memory allocation. To get an accurate measure, explicitly transferring the arrays to the device using the .to\_device methods delivered the numbers I was expecting.

Still, any confirmation here would be very appreciated! Thanks!

---

<div class="post-metadata">

**Author:** ![gmarkall](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/gmarkall/32/42_2.png) [@gmarkall](https://numba.discourse.group/u/gmarkall)\
**Post date:** [November 18, 2024, 8:58am UTC](https://numba.discourse.group/t/understanding-memory-usage-of-a-kernel/2790/3 "2024-11-18T08:58:54Z")

</div>

Your explanation sounds correct - thanks for following up with the answer!
