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

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

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

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

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

Related

  • Singleton: 6 Ways To Write and Use in Java Programming
  • All You Need To Know About Garbage Collection in Java
  • Java Memory Management
  • Heap Memory In Java Applications Performance Testing

Trending

  • Simpler Data Transfer Objects With Java Records
  • AI Agents: A New Era for Integration Professionals
  • Implementing Explainable AI in CRM Using Stream Processing
  • How to Introduce a New API Quickly Using Micronaut
  1. DZone
  2. Coding
  3. Languages
  4. How to Estimate Object Memory Allocation in Java

How to Estimate Object Memory Allocation in Java

This article shows three ways to estimate object memory allocation in Java. A comparison of all approaches and examples is included.

By 
Dmitry Egorov user avatar
Dmitry Egorov
DZone Core CORE ·
Mar. 05, 22 · Tutorial
Likes (11)
Comment
Save
Tweet
Share
12.9K Views

Join the DZone community and get the full member experience.

Join For Free

Estimating Allocated Memory (Not Object Size)

Previously, I explained how to calculate an object's size considering OS binary or types of objects and primitives. In this article, I'll only review ways to estimate the size of already allocated memory for a given object. There are a couple of ways to do it. Here we review the most popular.

Estimating Memory Using Profiler

The easiest way to estimate the memory of some objects is to look right inside JVM's memory using a profiler such as Visual VM. 

Visual VM screenshot

The problem with this approach is that you have to connect to a running JVM, which might be impossible for production environments due to security reasons.  

Estimating Memory Using Instrumentation

Another way to estimate allocated memory by a given object is to use Instruments tools. In short, we need to create a class and compile it to JAR. Once JAR is created, we have to execute our JVM together with that JAR. You can find details about this approach here. The disadvantage of this approach is the requirement to add a specific jar to JVM, and it might be not acceptable for production due to security or related issues.

Estimating Memory Using JOL Library

As another option, we can use JOL Library. It is a very powerful library and can provide detailed estimation about object weight and memory allocated by an object instance. To use the library, we need to add the dependency:

Plain Text
 
<dependency>
    <groupId>org.openjdk.jol</groupId>
    <artifactId>jol-core</artifactId>
    <version>0.16</version>
</dependency>

Afterward, we can use it like this:

Java
 
out.println(GraphLayout.parseInstance(myObject).totalSize() / 1024000d + " MB")

As a result, we will see the number of megabytes allocated by myObject with its internal content. Unfortunately, this approach will consume a lot of memory and time and is not suitable for big objects.

ObjectSizeCalculator From Twitter Archive

There is a tool class in the open-source Twitter GitHub repository that can estimate allocated memory for a given object instance ObjectSizeCalculator.  This solution doesn't eat a lot of memory or time. The estimation process takes seconds, even for big objects.  Usage of this class is pretty straightforward: 

Java
 
ObjectSizeCalculator.getObjectSize(address)

I recommend this approach, but keep in mind that it is supported only by Java Hotspot, OpenJDK, and TwitterJDK. 

Memory (storage engine) Object (computer science) Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Singleton: 6 Ways To Write and Use in Java Programming
  • All You Need To Know About Garbage Collection in Java
  • Java Memory Management
  • Heap Memory In Java Applications Performance Testing

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!