Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String vs char[]

I have some slides from IBM named : "From Java Code to Java Heap: Understanding the Memory Usage of Your Application", that says, when we use String instead of char[], there is

Maximum overhead would be 24:1 for a single character!

but I am not able to understand what overhead is referred here. Can anybody please help?

Source :

enter image description here

like image 583
codingenious Avatar asked Nov 20 '13 12:11

codingenious


People also ask

Which is better string or char array?

A char array is harder to manage than a string and certain functions may only accept a string as input, requiring you to convert the array to a string. It's better to use strings, they were made so that you don't have to use arrays. If arrays were objectively better we wouldn't have strings.

What is the difference between char array and string?

String is implemented to store sequence of characters and to be represented as a single data type and single entity. Character Array on the other hand is a sequential collection of data type char where each element is a separate entity. String internal implementation makes it immutable in nature.

Should I use char [] or std::string?

In C++ you should in almost all cases use std::string instead of a raw char array. std::string manages the underlying memory for you, which is by itself a good enough reason to prefer it.

Is char * the same as string?

The difference between a string and a char* is that the char* is just a pointer to the sequence. This approach of manipulating strings is based on the C programming language and is the native way in which strings are encoded in C++.


1 Answers

This figure relates to JDK 6- 32-bit.

JDK 6

In pre-Java-7 world strings which were implemented as a pointer to a region of a char[] array:

// "8 (4)" reads "8 bytes for x64, 4 bytes for x32"  class String{      //8 (4) house keeping + 8 (4) class pointer     char[] buf;    //12 (8) bytes + 2 bytes per char -> 24 (16) aligned     int offset;    //4 bytes                     -> three int     int length;    //4 bytes                     -> fields align to     int hash;      //4 bytes                     -> 16 (12) bytes } 

So I counted:

36 bytes per new String("a") for JDK 6 x32  <-- the overhead from the article 56 bytes per new String("a") for JDK 6 x64. 


JDK 7

Just to compare, in JDK 7+ String is a class which holds a char[] buffer and a hash field only.

class String{      //8 (4) + 8 (4) bytes             -> 16 (8)  aligned     char[] buf;    //12 (8) bytes + 2 bytes per char -> 24 (16) aligned     int hash;      //4 bytes                         -> 8  (4)  aligned } 

So it's:

28 bytes per String for JDK 7 x32  48 bytes per String for JDK 7 x64. 

UPDATE

For 3.75:1 ratio see @Andrey's explanation below. This proportion falls down to 1 as the length of the string grows.

Useful links:

  • Memory usage of Java Strings and string-related objects.
  • Calculate memory of a Map Entry - a simple technique to get a size of an object.
like image 137
Andrey Chaschev Avatar answered Sep 27 '22 20:09

Andrey Chaschev