Here is a common Java interview question:
Chat: The size of the String object in Java?
First, look at the space occupied by an empty String
The current memory size is under the condition that the compressed pointer is turned on by default
- object head 12
- char[] array reference 4
- int type hash data size 4
- loss due to the next object alignment alignment padding 4
summary: 24
Again, let's look at the member variables in the String class.
/** The value is used for character storage. */
private final char value[];
/** Cache the hash code for the string */
private int hash; // Default to 0
/** use serialVersionUID from JDK 1.0.2 for interoperability */
private static final long serialVersionUID = -6849794470754667710L;
Space occupied by a non-empty String
The current memory size is under the condition that the compressed pointer is turned on by default
- object head 12
- char[] array reference 4
- int type hash data size 4
- loss due to the next object alignment alignment padding 4
summary: 24
Recommended reading:
-
" Springcloud gateway underlying principles, core combat (the most complete in history) "
-
" Flux, Mono, Reactor combat (the most complete in history) "
-
" Sharding-JDBC underlying principle and core practice (the most complete in history) "
-
" clickhouse ultra-low-level principle + high-availability practice (the most complete in history) "
-
" Redis cluster practice (the most complete, 5w word long text in history) "
-
" Red-black tree (illustration + instant understanding + the most complete in history) "
-
" The King of Cache: The Use of Caffeine (The Most Complete in History) "
-
" Java Agent probe, bytecode enhanced ByteBuddy (the most complete in history) "
-
" Docker principle (illustration + second understanding + the most complete in history) "
-
" Redis Distributed Lock (Illustration - Second Understanding - The Most Complete in History) "
-
" Zookeeper Distributed Lock - Diagram - Second Understanding "
-
" Zookeeper Curator event monitoring - 10 minutes to understand "
-
" Netty Sticky Package Unpacking | The Most Complete Interpretation in History "
-
" Netty 1 million-level high concurrent server configuration "
-
" Springcloud high concurrency configuration (full understanding) "