How to Find the Memory Size of a NumPy Array?
The total bytes of memory occupied by the elements of the NumPy array is referred to as the memory size of the NumPy array. The memory size of a NumPy array can be found using the following methods:
- By using the itemsize and size attributes of the NumPy array.
- By using the nbytes attribute of the NumPy array.
Let's discuss how these methods are used to find the memory size of a NumPy array.
Transform Your Career
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
Techniques for Determining the Memory Size of NumPy Array
1. Making use of the itemsize and size attributes
- Size attribute is used for finding the size of an array by calculating the total number of elements present in an array.
The syntax for using the size attribute in a NumPy array is: ndarray.size
- Itemsize attribute is used for finding the size of each element of a numpy array in bytes.
The syntax for using the itemsize attribute in a numpy array is: ndarray.itemsize
Let's see the following examples:
Example 1 Code:
Output:
Explanation: Using the np.arange function, created a NumPy array arr of integer data type. By using the size and itemsize attributes, we get the total number of elements, i.e., 5, and the total bytes occupied by each element, i.e., 8. Now to get the total memory occupied by the NumPy array, we have to multiply the size and itemsize, i.e., 5x8, which returns 40 bytes as the total memory size of the array.
Example 2 Code:
Output:
Explanation: Created an array of 5 elements of complex datatype using the np.arange function. Each element of the Complex datatype requires 16 bytes of memory. The total memory occupied by this complex type NumPy array is 5x16, i.e. 80 bytes.
Example 3 Code:
Output:
Explanation: By using the size attribute, we get the total number of elements in the 2-D array of integer datatype and the memory occupied by each element by using the itemsize attribute of NumPy. Multiply both sizes and itemsize to find the total memory occupied by the NumPy array, i.e. 6x8, which gives 48 bytes as a result.
Scaler Placement Report and Statistics
Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.
2. Using the nbytes function
- nbytes is used for finding the total bytes occupied by the numpy array.
Syntax for using the nbytes function: ndarray.nbytes
Let's see the following examples:
Example 1 Code:
Output:
Explanation: Created an array of integer datatypes by using the np.arrange function. Using size and itemsize attributes, we get the total number of elements in the array and the number of bytes occupied by each element. With the nbytes attribute, we get the total memory occupied by the numpy array, i.e. 5x8=48 bytes.
Example 2 Code:
Output:
Explanation: In the above code example, there are a total of 6 elements in the 2-D array. Each element of integer type occupied 8 bytes of memory. By using the nbytes attribute of NumPy, we get the total memory size of a NumPy array is 6x8, which is 48 bytes.
Turn Learning into Career Growth
Conclusion
- The total bytes occupied by the elements of an array are referred to as the memory size of the NumPy array.
- One method for determining the memory size of a NumPy array is to use the itemsize and size attributes together.
- Using the nbytes attribute is another way of finding the memory size of a NumPy array.




