Best alternative to String lightweight implementation in Java


and:

我的应用程序是带有密集字符串处理的多线程。我们正在经历过多的内存消耗,并且性能分析表明这是由于String数据引起的。我认为使用某种flyweight模式实现甚至是缓存将极大地受益于内存消耗(我可以肯定Strings通常是重复的,尽管我在这方面没有任何硬数据)。

我看过Java常量池和String.intern,但似乎可以引发一些PermGen问题。

在Java中实现应用程序范围的多线程字符串池的最佳替代方法是什么?

编辑:另请参阅我以前的相关问题:Java如何在引擎盖下实现字符串的flyweight模式?

马克·彼得斯:

注意:此答案使用的示例可能与现代运行时JVM库无关。特别是,该substring示例在OpenJDK / Oracle 7+中不再是问题。

我知道这与人们经常告诉您的内容背道而驰,但是有时显式创建新String实例可能是减少内存的重要方法。

由于字符串是不可变的,因此有几种方法可以利用该事实并共享支持字符的数组以节省内存。但是,有时这可以通过防止垃圾回收那些数组未使用的部分来实际上增加内存。

例如,假设您正在解析日志文件的消息ID,以提取警告ID。您的代码如下所示:

//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...
}

但是看看实际存储的数据:

    //...
    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) );

这就是整个测试行,因为匹配器只是在相同的字符数据周围包装了一个新的String实例。当您更换比较结果String id = matcher.group(1);String id = new String(matcher.group(1));

Related


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 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