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. Coding
  3. Java
  4. Java: How much memory do different arrays consume

Java: How much memory do different arrays consume

Peter Lawrey user avatar by
Peter Lawrey
·
Jul. 29, 11 · Interview
Like (0)
Save
Tweet
Share
14.96K Views

Join the DZone community and get the full member experience.

Join For Free
Arrays can be large and consume a significant amount of memory. It can me worth choosing the most memory efficient array/collection.

Comparing array sizes

How much space does a `new int[1024]` take compared with an `new Integer[1024]`?
int[] ints = new int[1024];
for (int i = 0; i < ints.length; i++) ints[i] = i;
compared with
Integer[] ints = new Integer[1024];
for (int i = 0; i < ints.length; i++) ints[i] = i;
Note: 1/8th of Integer values will come from the auto-box cache and not use additional memory. All possible Boolean and Byte values are cached.
arraysize in bytes
32-bit JVM
size in bytes
64-bit JVM
new BitSet(1024)168168
new boolean[1024]10401040
new Boolean[1024]41124112
new ArrayList<Boolean>(1024)41364136
new LinkedList<Boolean>() with 10242462424624
new byte[1024]10401040
new Byte[1024]41124112
new ArrayList<Byte>(1024)41364136
new LinkedList<Byte>() with 10242462424624
new char[1024]20642064
new Character[1024]1844818448
new short[1024]20642064
new Short[1024]1844818448
new ArrayList<Character/Short>(1024)1847218472
new LinkedList<Character/Short>() with 10243896038960
new int[1024]41124112
new Integer[1024]1844818448
new float[1024]41124112
new Float[1024]2049620496
new ArrayList<Integer/Float>(1024)1847218472
new LinkedList<Integer/Float>() with 10243896038960
new long[1024]82088208
new Long[1024]1844825616
new double[1024]82088208
new Double[1024]2049628688
new ArrayList<Long/Double>(1024)1847225640
new LinkedList<Long/Double>() with 10243896046128
new String[1024]5246461456
new ArrayList<String>(1024)5248861480
new LinkedList<String>() with 10247297681968
In both cases, Java 6 update 26 was used. For the 64-bit JVM of this version, -XX:+UseCompressedOops is the default option which uses 32-bit references. If you use more than 32 GB of memory, you would need to turn this option off, using more memory for the arrays of objects.

The full code

Is available here: MemoryUsageExamplesTest

From http://vanillajava.blogspot.com/2011/07/java-how-much-memory-do-different.html

Memory (storage engine) Java (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Spring Boot vs Eclipse Micro Profile: Resident Set Size (RSS) and Time to First Request (TFR) Comparative
  • Unlocking the Power of Elasticsearch: A Comprehensive Guide to Complex Search Use Cases
  • The 5 Books You Absolutely Must Read as an Engineering Manager
  • Create a REST API in C# Using ChatGPT

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: