Flyweight: Strings already use String pool: does it make sense to combine String objects for Flyweight?

Lines already use the Flyweight design pattern. Will it be useful / efficient to combine common String objects. Since the rows will be pulled from the row pool?

+4
source share
2 answers

Without any other information about your system, I would say that creating a pool of specific goals for strings would fall into the category of premature optimization. If your system is really very heavy and string profiling shows that String objects are causing large garbage collectors to occur, I would recommend looking at StringBuilder as a replacement, as well as understanding better the best practices for working with cache strings for them.

+2
source

Strings can come from many places, and by default only string literals are in the string pool. For example, when you call BufferedReader.readLine() , the returned row is not in the row pool.

Whether it makes sense to combine such strings using String.intern() or a String.intern() map depends on how much duplication you have and how much memory you can save to reduce this duplication.

For example, if you are reading an XML file, it can be very useful to canonicalize element names. If you are reading a file with address data, it may be useful to canonize zip codes and / or city names. However, in both cases, I would look at using Map , rather than calling intern() , because the latter consumes permgene memory (which is a scarce resource than regular heap memory).

+6
source

Source: https://habr.com/ru/post/1346119/


All Articles