Categories: JavaWeb

Java – How to Calculate Size of Objects & Arrays

This article represents a list of web pages which can help one understand the memory usage of Java objects and arrays along with examples. Please feel free to comment/suggest any other cool pages. Also, sorry for the typos.
The in-memory size of the object depends on the architecture, mainly on whether the VM is 32 or 64-bit. The actual VM implementation also matters.
  • How to calculate memory usage of a Java object?: Very simplified explanation of how one could calculate a memory of any Java object. For example, lets say, you want to calculate the memory of a Java object which holds two int variables, one boolean variable, one Long object, and a reference to other object. The memory would turn out to be following:
    • 8 bytes for the object header
    • 2 x 4 = 8 bytes for two int variables
    • 1 byte for a boolean variable
    • 8 bytes (object reference) + 8 bytes for long data type = 16 bytes for long object
    • 4 bytes for reference to some other object

    The total size of the above mentioned object will be 8 + 8 + 1 + 16 + 4 = 37 bytes + 3 bytes (for padding) = 40 bytes.

  • How to calculate memory usage of a Java array?: The page presents examples on how to calculate size of a Java array object. For example, lets say a Java array consisting of 20 Integer objects. Following is the detail on the size:
    • 12 bytes for array header object (8 bytes for header and 4 bytes for storing length of the array)
    • 20 x 16 bytes = 320 bytes for integer objects.

    The total size of the said Java array object = 12 + 320 bytes = 332 bytes + 4 bytes (padding) = 336 bytes.

  • Memory usage of both Java Objects and Array: This article presents more examples on memory usage of obejcts types such as String.
Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. For latest updates and blogs, follow us on Twitter. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking. Check out my other blog, Revive-n-Thrive.com

View Comments

Recent Posts

Bias-Variance Trade-off in Machine Learning: Examples

Last updated: 1st May, 2024 The bias-variance trade-off is a fundamental concept in machine learning…

5 hours ago

Mean Squared Error vs Cross Entropy Loss Function

Last updated: 1st May, 2024 As a data scientist, understanding the nuances of various cost…

5 hours ago

Cross Entropy Loss Explained with Python Examples

Last updated: 1st May, 2024 In this post, you will learn the concepts related to…

5 hours ago

Logistic Regression in Machine Learning: Python Example

Last updated: 26th April, 2024 In this blog post, we will discuss the logistic regression…

5 days ago

MSE vs RMSE vs MAE vs MAPE vs R-Squared: When to Use?

Last updated: 22nd April, 2024 As data scientists, we navigate a sea of metrics to…

6 days ago

Gradient Descent in Machine Learning: Python Examples

Last updated: 22nd April, 2024 This post will teach you about the gradient descent algorithm…

1 week ago