DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Singleton: 6 Ways To Write and Use in Java Programming
  • Redefining Java Object Equality
  • Exploring Exciting New Features in Java 17 With Examples
  • Creating a Deep vs. Shallow Copy of an Object in Java

Trending

  • How to Ensure Cross-Time Zone Data Integrity and Consistency in Global Data Pipelines
  • Understanding IEEE 802.11(Wi-Fi) Encryption and Authentication: Write Your Own Custom Packet Sniffer
  • Unit Testing Large Codebases: Principles, Practices, and C++ Examples
  • How Kubernetes Cluster Sizing Affects Performance and Cost Efficiency in Cloud Deployments
  1. DZone
  2. Coding
  3. Languages
  4. Calculating the Memory Usage of Objects and Arrays

Calculating the Memory Usage of Objects and Arrays

Curious as to how Java uses memory to store objects and arrays? Read on to find out some of the math behind it and more information on just how this is done.

By 
Ajitesh Kumar user avatar
Ajitesh Kumar
·
Nov. 07, 16 · Tutorial
Likes (24)
Comment
Save
Tweet
Share
57.9K Views

Join the DZone community and get the full member experience.

Join For Free

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 objects. 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 objects types such as String.
Object (computer science) Memory (storage engine) Data Types Java (programming language)

Published at DZone with permission of Ajitesh Kumar, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Singleton: 6 Ways To Write and Use in Java Programming
  • Redefining Java Object Equality
  • Exploring Exciting New Features in Java 17 With Examples
  • Creating a Deep vs. Shallow Copy of an Object in Java

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!