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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
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
Partner Zones AWS Cloud
by AWS Developer Relations
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
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Java Object Memory Layout / Anatomy —Deep Size of an Object

Java Object Memory Layout / Anatomy —Deep Size of an Object

Even to the mind of the most experienced Java Developer, the way JVM organizes or allocates memory for an Object may not come intuitively. Memory layout is key.

Sumith Puri user avatar by
Sumith Puri
·
Jun. 16, 15 · Tutorial
Like (3)
Save
Tweet
Share
13.95K Views

Join the DZone community and get the full member experience.

Join For Free

even to the mind of the most experienced java developer, the way jvm organizes or allocates memory for an object may not come intuitively. this also includes activities like measuring the size of an object.

when we use 'new' to instantiate an object, there is much more data size allocated than is required to just hold the value. for example, if you choose to create java.lang.integer, the actual int value will be only 1:4 parts of the object. the remaining is used to hold the metadata - the metadata includes the following:

1. class: a pointer to the type of the class. in our case, a pointer to the java.lang.integer class.

2. flags: a collection of flags that describe the state of the object, including the hash code of the object and the shape of the object (if it is an array or not)

3. lock: the synchronization information of the object, including if it is synchronized currently.


the object metadata is followed by the actual object data. in this specific case, an int value.


example layout of a java.lang.integer object for a 32-bit java process


example layout of a java.lang.integer object for a 32-bit java process




[reference: http://www.ibm.com/developerworks/library/j-codetoheap/]


i tried to explore all possibilities to measure the object memory usage as per the above understanding of the object allocation. there are many tools, blogs and write-ups on the internet, but all of them provide approximation, which may not be reliably usable. also, the values which may be returned by each of these will not concur across these tools.

the only tool that i came across that seems to be reliable and has the source code public is the java agent for memory measurements [https://github.com/jbellis/jamm]. the author also seems to be the committer for the cassandra project, and it is being used in cassandra - hence, it is a safe bet and should be close to as exact as possible. i went through the source and the logic seems to concur with the memory layout above, also, since it is being used in very important (and widely used) projects, [i skipped trying it out myself]. in all, this should be your safest [reliablity and performance] bet if you want to use it to introduce custom cache measurements, custom object profiling, server profiling or simply memory profiling tools.

[i plan to write my own tool to measure object sizes in java as reliably as possible - using the above understanding of java memory allocation . keep checking this blog to download the source and binary]

Threading Memory (storage engine) Java (programming language)

Published at DZone with permission of Sumith Puri. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 10 Best Ways to Level Up as a Developer
  • The 5 Books You Absolutely Must Read as an Engineering Manager
  • Is DevOps Dead?
  • What Is API-First?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: