# How to know the memory allocated for a tensor on gpu?

**URL:** <https://discuss.pytorch.org/t/how-to-know-the-memory-allocated-for-a-tensor-on-gpu/28537>\
**Category:** Uncategorized\
**Created:** [November 1, 2018, 9:24pm UTC](https://discuss.pytorch.org/t/how-to-know-the-memory-allocated-for-a-tensor-on-gpu/28537 "2018-11-01T21:24:13Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![albanD](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alband/32/215_2.png) [@albanD](https://discuss.pytorch.org/u/albanD)\
**Post date:** [November 2, 2018, 10:54am UTC](https://discuss.pytorch.org/t/how-to-know-the-memory-allocated-for-a-tensor-on-gpu/28537/2 "2018-11-02T10:54:46Z")

</div>

Hi,

`sys.getsizeof()` will return the size of the python object. It will the same for all tensors as all tensors are a python object containing a tensor.  
For each tensor, you have a method `element_size()` that will give you the size of one element in byte. And a function `nelement()` that returns the number of elements.  
So the size of a tensor `a` in memory (cpu memory for a cpu tensor and gpu memory for a gpu tensor) is `a.element_size() * a.nelement()`.

All objects are store in cpu memory. The only thing that can be using GPU memory are tensors (from all pytorch objects). So the gpu memory used by whatever object is the memory used by the tensors on the gpu that it contains.

---

_[View the full topic](https://discuss.pytorch.org/t/how-to-know-the-memory-allocated-for-a-tensor-on-gpu/28537)._
