In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to understand string constant pool". The content of the explanation is simple and clear, and it is easy to learn and understand. let's follow the editor's train of thought to study and learn "how to understand string constant pool".
String redundancy problem
On average, String objects in an application consume a lot of memory. Some of this is redundant-there will be multiple different instances of the same string (a! = b, but a.equals (b)). In practice, there are many strings that cause redundancy for different reasons.
Initially, JDK provided a String.intern () method to solve the problem of string redundancy. The drawback of this approach is that you have to find out which strings need to be interned. You usually need a heap analysis tool with redundant string lookup capabilities, such as Youkit profiler. String residency can be a very effective memory-saving tool if used properly-it allows you to reuse entire string objects (each string object adds 24 bytes of overhead to the underlying char []).
Starting with Java 7 update 6, each String object has its own private char []. In this way, JVM can automatically optimize-since the underlying char [] is not exposed to external clients, JVM can determine whether the contents of two strings are consistent, and then replace the underlying char [] of one string with the underlying char [] array of another string.
The string deduplication feature is used to do this, and it was introduced in Java 8 update 20. Here's how it works:
1. You have to use the G1 garbage collector and enable this feature: the-XX:+UseG1GC-XX:+UseStringDeduplication feature is implemented as an optional step in the G1 garbage collector, which is not available if you are using another garbage collector.
two。 This feature is implemented in the minor GC phase of the G1 recycler. From my observation, whether it will be executed depends on how many free CPU cycles there are. Therefore, you can't expect it to be performed in a data analyzer that handles local data, that is, this optimization is likely to be performed in the WEB server.
3. String deduplication looks for unprocessed strings, calculates their hash value (if it hasn't been calculated in the applied code), and then sees if any other strings have the same hash value as the underlying char []. If found-- it replaces the existing char [] with the char [] of a new string.
4. String deduplication only deals with strings that are still alive after several times of GC. This ensures that most short-lifecycle strings are not processed. The minimum age of survival for a string can be specified by the JVM parameter of-XX:StringDeduplicationAgeThreshold=3 (3 is the default value for this parameter).
Here are some important conclusions of this implementation:
Yes, if you want to enjoy this free lunch of string de-repetition features, you have to use the G1 recycler.
It is not possible to use parellel GC, and parellel GC should be a better choice for applications that require higher throughput than latency.
String deduplication cannot be run on a loaded system. To know if it has been executed, run JVM with the-XX:+PrintStringDeduplicationStatistics parameter and look at the console output.
If you want to save memory, you can interned strings in your application-then go ahead and don't rely on string deduplication.
What you need to keep in mind is that string deduplication deals with all your strings (at least most of them)-that is, although you know that the contents of a given string are unique (such as GUID), but JVM doesn't know that, it will try to match this string with other strings. The result is that the CPU overhead generated by string deduplication depends both on the number of strings in the heap (comparing new strings with other strings) and on the number of strings you create in the interval between string deduplications (these strings are compared to strings in the heap).
On a JVM with several gigabytes of heap, you can use the-XX:+PrintStringDeduplicationStatistics option to see how much impact this feature has.
On the other hand, it's basically done in a non-blocking way, so if your server has enough free CPU, why not use it?
Finally, keep in mind that String.intern allows you to generate redundant strings only for a specified part of your application. In general, it only needs to compare a pool of smaller resident strings, which means you can use your CPU more efficiently. Not only that, you can also host the entire string object, which saves you an extra 24 bytes per string.
Here is a test class that I use to test this feature. All three tests run until JVM throws OOM, so you have to run them separately.
The first test creates a string with the same content, which is very useful if you want to know how long it will take to deduplicate a string when there are many strings in the heap. Try to allocate as much memory as possible to the first test-the more strings it creates, the better the optimization will be.
The second and third tests compare the difference between de-weight (second test) and resident (interning, third test). You have to run them with the same Xmx settings. I set this constant to Xmx256M in the program, but of course, you can assign more. However, you will find that de-retesting will fail earlier than the interning test. Why is that? Because we only have 100 different strings in this set of tests, hosting them means that the memory you use is just the space needed to store these strings. If the string is deduplicated, it will result in a different string object, which only shares the underlying char [] array.
/ * String deduplication vs interning test * / public class StringDedupTest {private static final int MAX_EXPECTED_ITERS = 300; private static final int FULL_ITER_SIZE = 100 * 1000; / / 30m entries = 120m RAM (for 300 iters) private static List LIST = new ArrayList (MAX_EXPECTED_ITERS * FULL_ITER_SIZE) Public static void main (String [] args) throws InterruptedException {/ / 24024 bytes per String (24 String shallow, 24 char []) / / 136m left for Strings / / Unique, dedup / / 136m / 2.9m strings = 48 bytes (exactly String size) / / Non unique, dedup / / 4.9m Strings 100 char [] / / 136m / 4.9m strings = 27.75bytes (close to 24 bytes per String + small overhead / / Non unique, intern / / We use 120m (+ small overhead for 100strings) until very late, but can't extend ArrayList 3 times-we don't have 360M / * Run it with:-XX:+UseG1GC-XX:+UseStringDeduplication-XX:+PrintStringDeduplicationStatistics Give as much Xmx as you can on your box. This test will show you how long does it take to run a single deduplication and if it is run at all. To test when deduplication is run, try changing a parameter of Thread.sleep or comment it out. You may want to print garbage collection information using-XX:+PrintGCDetails-XX:+PrintGCTimestamps * / / Xmx256M-29 iterations fillUnique (); / * This couple of tests compare string deduplication (first test) with string interning. Both tests should be run with the identical Xmx setting. I have tuned the constants in the program for Xmx256M, but any higher value is also good enough. The point of this tests is to show that string deduplication still leaves you with distinct String objects, each of those requiring 24 bytes. Interning, on the other hand, return you existing String objects, so the only memory you spend is for the LIST object. * / / Xmx256M-49 iterations (100unique strings) / / fillNonUnique (false); / / Xmx256M-299iterations (100unique strings) / / fillNonUnique (true);} private static void fillUnique () throws InterruptedException {int iters = 0; final UniqueStringGenerator gen = new UniqueStringGenerator (); while (true) {for (int I = 0; I < FULL_ITER_SIZE) + + I) LIST.add (gen.nextUnique ()); Thread.sleep (300); System.out.println ("Iteration" + (iters++) + "finished");}} private static void fillNonUnique (final boolean intern) throws InterruptedException {int iters = 0; final UniqueStringGenerator gen = new UniqueStringGenerator (); while (true) {for (int I = 0) I < FULL_ITER_SIZE; + + I) LIST.add (intern? Gen.nextNonUnique (). Intern (): gen.nextNonUnique (); Thread.sleep (300); System.out.println ("Iteration" + (iters++) + "finished");}} private static class UniqueStringGenerator {private char upper = 0; private char lower = 0; public String nextUnique () {final String res = String.valueOf (upper) + lower If (lower < Character.MAX_VALUE) lower++; else {upper++; lower = 0;} return res;} public String nextNonUnique () {final String res = "a" + lower If (lower < 100) lower++; else lower = 0; return res;}}
String deduplication has been added to Java 8 update 20. It is part of the G1 garbage collector, so you must use the G1 collector to enable it:-XX:+UseG1GC-XX:+UseStringDeduplication
String deduplication is an optional phase of G1. It depends on the current system load.
String deduplication queries the same strings and unifies the char [] array of their underlying stored characters. You don't need to write any code to use this feature, but this means that you end up with a different string object, each taking up 24 bytes. Sometimes it is necessary to explicitly call String.intern to host.
String deduplication does not deal with young strings. The minimum age for string processing is managed through the JVM parameter of-XX:StringDeduplicationAgeThreshold=3 (3 is the default value for this parameter)
Thank you for your reading. the above is the content of "how to understand string constant pool". After the study of this article, I believe you have a deeper understanding of how to understand string constant pool. the specific use also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.