Best alternative to String lightweight implementation in Java


and:

My application is multithreaded with intensive string processing. We are experiencing excessive memory consumption and profiling shows that it is due to String data. I think implementing or even caching with some kind of flyweight pattern would greatly benefit from memory consumption (I'm pretty sure Strings are usually duplicates, although I don't have any hard data on this).

I've looked at Java constant pools and String.intern, but it seems to cause some PermGen issues.

What is the best alternative to implement an application-wide multithreaded string pool in Java?

Edit: See also my previous related question: How does Java implement the flyweight pattern for strings under the hood?

Mark Peters:

NOTE: The examples used in this answer may not be relevant to modern runtime JVM libraries. In particular, the substringexample is no longer a problem in OpenJDK/Oracle 7+.

I know this goes against what people often tell you, but sometimes explicitly creating new Stringinstances can be an important way to reduce memory.

Since strings are immutable, there are several ways to take advantage of that fact and share an array of backing characters to save memory. However, sometimes this can actually increase memory by preventing garbage collection of those unused parts of the array.

For example, let's say you're parsing a log file for message IDs to extract warning IDs. Your code looks like this:

//Format:
//ID: [WARNING|ERROR|DEBUG] Message...
String testLine = "5AB729: WARNING Some really really really long message";

Matcher matcher = Pattern.compile("([A-Z0-9]*): WARNING.*").matcher(testLine);
if ( matcher.matches() ) {
    String id = matcher.group(1);
        //...do something with id...
}

But look at the actual stored data:

    //...
    String id = matcher.group(1);
    Field valueField = String.class.getDeclaredField("value");
    valueField.setAccessible(true);

    char[] data = ((char[])valueField.get(id));
    System.out.println("Actual data stored for string \"" + id + "\": " + Arrays.toString(data) );

That's the whole test line, because the matcher just wraps a new String instance around the same character data. String id = matcher.group(1);Use when you replace the comparison result String id = new String(matcher.group(1));.

Related


Best alternative to String lightweight implementation in Java

and: 我的应用程序是带有密集字符串处理的多线程。我们正在经历过多的内存消耗,并且性能分析表明这是由于String数据引起的。我认为使用某种flyweight模式实现甚至是缓存将极大地受益于内存消耗(我可以肯定Strings通常是重复的,尽管我在这方面没有任何硬数据)。 我看过Java常量池和String.intern,但似乎可以引发一些PermGen问题。 在Java中实现应用程序范围的多线程字符串池的最佳替代方法是什么? 编辑:另请参阅我以前的相关问题:Java如何在引擎盖下实现字符串的flyweight模式

Best alternative to String lightweight implementation in Java

and: My application is multithreaded with intensive string processing. We are experiencing excessive memory consumption and profiling shows that it is due to String data. I think implementing or even caching with some kind of flyweight pattern would greatly be

Best implementation of Java Queue?

Georges Oates Larsen: I'm (in Java) working on a recursive image processing algorithm that recursively traverses the pixels of an image from a center point outwards. Unfortunately this leads to a stack overflow. Therefore, I decided to switch to a queue-based

Best implementation of Java Queue?

Georges Oates Larsen: I'm (in Java) working on a recursive image processing algorithm that recursively traverses the pixels of an image from a center point outwards. Unfortunately this leads to a stack overflow. Therefore, I decided to switch to a queue-based

Best implementation of Java Queue?

Georges Oates Larsen: I'm (in Java) working on a recursive image processing algorithm that recursively traverses the pixels of an image from a center point outwards. Unfortunately this leads to a stack overflow. Therefore, I decided to switch to a queue-based

Best alternative to switch statement in Java

Anderson: The switch statement is great when we have 5 switch cases, but in our case we have 15 switch cases. I would like to know what is the best alternative to switch statement in Java private OpwaardernId getOpwaardernId(String opId) { OpwaardernId op

Best alternative to switch statement in Java

Anderson: The switch statement is great when we have 5 switch cases, but in our case we have 15 switch cases. I would like to know what is the best alternative to switch statement in Java private OpwaardernId getOpwaardernId(String opId) { OpwaardernId op

Best alternative to switch statement in Java

Anderson: The switch statement is great when we have 5 switch cases, but in our case we have 15 switch cases. I would like to know what is the best alternative to switch statement in Java private OpwaardernId getOpwaardernId(String opId) { OpwaardernId op

Best alternative to switch statement in Java

Anderson: The switch statement is great when we have 5 switch cases, but in our case we have 15 switch cases. I would like to know what is the best alternative to switch statement in Java private OpwaardernId getOpwaardernId(String opId) { OpwaardernId op

Best alternative to switch statement in Java

Anderson: The switch statement is great when we have 5 switch cases, but in our case we have 15 switch cases. I would like to know what is the best alternative to switch statement in Java private OpwaardernId getOpwaardernId(String opId) { OpwaardernId op

Lightweight alternative to Hibernate?

Jared: I have a user Java program that wants to store data in a lightweight database such as Derby or Sqlite. I want to use a data abstraction layer in my program. Hibernate seems to require a lot of configuration and is overkill for what I need. What is a lig

Lightweight alternative dock

Alex: Generally , Jetty is known as a lightweight alternative when it comes to servlet containers like Tomcat and App Servers like Glassfish . I want to run a RESTful service on CloudFoundry. use jetty java -jar target/dependency/jetty-runner.jar target/*.war

Lightweight GNU readline alternative

punekr12 : I am looking for a GNU readline alternative. It has many features but only a few of them worked for me as described below - I am developing an interactive command prompt application (displays the prompt and accepts the next user command to run). I w

Lightweight multilingual alternative to JMX?

ROM1: I have a software real-time application project that requires extensive monitoring. JMX seems to be a good fit for the task, it's just that the application project is based on c++. Is there a lightweight alternative to JMX (with a c/c++ support library)

Lightweight alternative dock

Alex: Generally , Jetty is known as a lightweight alternative when it comes to servlet containers like Tomcat and App Servers like Glassfish . I want to run a RESTful service on CloudFoundry. use jetty java -jar target/dependency/jetty-runner.jar target/*.war

Lightweight alternative to Hibernate?

Jared: I have a user Java program that wants to store data in a lightweight database such as Derby or Sqlite. I want to use a data abstraction layer in my program. Hibernate seems to require a lot of configuration and is overkill for what I need. What is a lig

Lightweight GNU readline alternative

punekr12 I am looking for a GNU readline alternative. It has many features but only a few of them worked for me as described below - I am developing an interactive command prompt application (displays the prompt and accepts the next user command to run). I wan

Lightweight GNU readline alternative

punekr12 : I am looking for a GNU readline alternative. It has many features but only a few of them worked for me as described below - I am developing an interactive command prompt application (displays the prompt and accepts the next user command to run). I w

A clean, lightweight alternative to Python?

jkp : One (a long time ago) I wrote a web spider and I multithreaded the thread to allow concurrent requests to happen at the same time. That was my Python youth, before I learned about the GIL and its associated troubles for multithreaded code (IE, most of th

Lightweight multilingual alternative to JMX?

ROM1: I have a software real-time application project that requires extensive monitoring. JMX seems to be a good fit for the task, it's just that the application project is based on c++. Is there a lightweight alternative to JMX (with a c/c++ support library)

Lightweight zxcvbn alternative?

Greg Blass Wondering if there are any lightweight zxcvbn alternatives out there. I don't need my password strength meter to be incredible - rather, I need to reduce the size of my assets. Can someone say 80% of the content? Maybe not the whole dictionary in th

Lightweight alternative dock

Alex Generally , Jetty is known as a lightweight alternative when it comes to servlet containers like Tomcat and App Servers like Glassfish . I want to run a RESTful service on CloudFoundry. use jetty java -jar target/dependency/jetty-runner.jar target/*.war

Lightweight alternative to Hibernate?

Jared: I have a user Java program that wants to store data in a lightweight database such as Derby or Sqlite. I want to use a data abstraction layer in my program. Hibernate seems to require a lot of configuration and is overkill for what I need. What is a lig

Lightweight alternative dock

Alex: Generally , Jetty is known as a lightweight alternative when it comes to servlet containers like Tomcat and App Servers like Glassfish . I want to run a RESTful service on CloudFoundry. use jetty java -jar target/dependency/jetty-runner.jar target/*.war

Lightweight GNU readline alternative

punekr12 : I am looking for a GNU readline alternative. It has many features but only a few of them worked for me as described below - I am developing an interactive command prompt application (displays the prompt and accepts the next user command to run). I w

A clean, lightweight alternative to Python?

jkp : One (a long time ago) I wrote a web spider and I multithreaded the thread to allow concurrent requests to happen at the same time. That was my Python youth, before I learned about the GIL and its associated troubles for multithreaded code (IE, most of th

Lightweight multilingual alternative to JMX?

ROM1: I have a software real-time application project that requires extensive monitoring. JMX seems to be a good fit for the task, it's just that the application project is based on c++. Is there a lightweight alternative to JMX (with a c/c++ support library)

A clean, lightweight alternative to Python?

jkp : One (a long time ago) I wrote a web spider and I multithreaded the thread to allow concurrent requests to happen at the same time. That was my Python youth, before I learned about the GIL and its associated troubles for multithreaded code (IE, most of th

Lightweight alternative to fork() in POSIX C?

Kyle From the man pages I'm reading, it seems that popen, system, etc. tend to call fork(). In turn, fork() copies the entire memory state of the process. This does seem like a lot of work, especially in many cases where the child calling fork() barely has any