Z34

You might also like

Download as pdf
Download as pdf
You are on page 1of 7
oart2023 20:97 ‘Compact Strngs in Java 8 | Baeldung “ Compact Strings in Java 9 (On Pemice SharePoint v Shared Online Comment chase? | $ row scrne \Written by. baeldung (https //wwwlbaeldung.com/author/oae\dung) Java String (https:// www.baeldung.com/category/java/java-string) Java 9 (https://wwwbaeldung.com/tag/java-9) Get started with Spring 5 and Spring Boot 2, through the Learn Spring course: >» CHECK OUT THE COURSE (/Is-course-start) nitpsdlwww-baeldung.comava-S-compacttring we oart2023 20:97 ‘Compact Stnngs in Java 8 | Baeldung ihttps//adsfreestarcom/? tm _campaign-brandingauutmmedium-banner8utm -source-baeldung.com&utm_content-baeldung_incontent =) . Overview Stringsin Java are internally represented by a chal! containing the characters of the String. And, every charis made up of 2 bytes because Java internally uses UTF-16, For instance. ifa String contains a word in the English language, the leading & bits will all be 0 for every char, as an ASCII character can be represented using a single byte. Many characters require 36 bits to represent thom but statistically most require only 8 bits — LATIN-1 character representation. So, there is a scope to improve the memory consumption and performance. ‘What's also important is that Strings typically usually occupy a large proportion of the JVM heap space. And, because of the way theyre stored by the JVM, in most cases, a String instance can take up double space It actually needs, Inthis article, well discuss the Compressed String option. introduced in JOKE and the new Compact String, recently introduced with JDK9. Both of these were designed to optimize memory consumption of Stings on the JMV. Deliver Tailored Multi-Brand Experiences Mp romero ne (https /adsfreestarcom/? 2. Compressed String - Java 6 The JDK 6 update 21 Performance Release, introduced a new VM option: ~x:sUseconpressedserings oO ‘When this option is enabled, Strings are stored as bytell instead of charil -thus, saving a lot of memory. However this option was eventually removed in JDK 7. mainly because it had some unintended performance consequences. 3. Compact String - Java 9 Java g has brought the concept of compact Strings back. ‘This means that whenever we create a Stringiif all the characters of the String can be represented using a byte — LATIN-1 representation, a byte array will be used internally, such that one byte is given for one character In other cases, ifany character requires more than 8-bits to represent it all the characters are stored using two bytes for each — UTF-16 representation, itpsutmewbaeldung.comljave-S-compactstring an oart2023 20:97 ‘Compact Stnngs in Java 8 | Baeldung “ . (https /adssreestarcom/? ‘So basically, whenever possible. itl ust use a single byte for each character Now, the question is - how will all the String operations work? How willit distinguish between the LATIN-1 and UTF-16 roprosontations? ‘Well, to tackle this issue, another change is field coder, that preserves this information. ‘made to the internal implementation of the String. We have a final 3.1. String Implementation in Java 9 Until now, the String was stored as a char private firal cher) values oO From now on. itllbe a byte: [ prs ra pe wate Oo ‘The variable coder [rm et = hor the coder canbe state final byte arnt = 6 Stetle finel byte UIFie = 1s 0 Most of the String operations now check the coder and dispatch to the specific implementation: itpsutmewbaeldung.comljave-S-compactstring an oart2023 20:37 ‘Compact Stnngs in Java 8 | Baeldung Fe ta Frontend oO wablie Yn ino return dsLatini() 7 Steinglavini.indexOF (value, ch, fromtndex) StringuTFis. index0# (value, ch, frostndex): y private boolean dsLatini() { Feturn COMPACT_STRINGS Lt cod , ‘With all. the info the JVM needs ready and available, the CompactString VM option is enabled by default, To sable it, we can use: soxsconpactsringe O 3.2, How coder Works In Java g String class implementation, the lengths calculated as: jolie Ire teeth) ¢ pacar vue tongth >> coders oO , Ifthe String contains only LATIN-1, the value of the coder will be 0 so the length of the String willbe the same as the length of the byte array. Inother cases, if the Strings in UTF-26 representation, the value of coder will be 1, and hence the length willbe half the size of the actual byte array Note that all the changes made for Compact String, are in the internal implementation of the String class and are fully transparent for developers using String. cr ® 1SK:inligence aif « antstnete Letestringd) + rstestring)§ (String) strings. stream) cotalTine = Systen.currentTinelittis() ~ startTines Systen.out.printin(*Created string of Length * + appended. Length) Sim + totalrine + "m8." ; Here, we are creating 10 million Strings and then appending them in a naive manner. When we run this code (Compact Strings are enabled by default). we get the output Generaved 19098909 strings in 854 ms Creates string of Tength s88895 in $130 ms. O Similarly, ifwe run it by disabling the Compact Strings using: -XX-CompactStrings option, the output is: Crested string of Leng s0838 In 727s. i Clearly, this is a surface level test, and it cant be highly representative — its only a snapshot of what the new option may do to improve performance in this particular scenario, 5. Conclusion Inthis tutorial, we saw the attempts to optimize the performance and memory consumption on the JVM ~ by storing Strings in a memory efficient way. As always, the entire code is available ever on Github (htps//github.com/eugenp/tutorials/trae/master/core- java-modules/core-java-string-operations-6). itpsutmewbaeldung.comljave-S-compactstring oart2023 20:37 ‘Compact Stnngs in Java 8 | Baeldung “ Get started with Spring 5 and Spring Boot 2, through the Learn Spring course: >» CHECK OUT THE COURSE (/1s-course-ond) Learning to build your API with Spring? Downtoad the E-book (/rest-api-spring-guide) Comments are closed on this article! ‘COURSES SERIES httpsifweww.bacldung,comjava-S-compactstring oart2023 20:97 ‘Compact Stnngs in Java 8 | Baeldung poms han es ‘ABOUT itpsutmewbaeldung.comljave-S-compactstring

You might also like