Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will the String passed from outside the java application be saved in String pool?

I read many answers but none of them really answers my question exactly.

If I've a java service running on some port and a client connects to it and calls a method like:

String data = getServiceData("clientKey");

Now my question is, will this key(clientKey) be stored in String literal pool on service side? Generally literals to be stored in constant pools are figured out at compile time but what happens to strings that are passed from outside the JVM or may be while reading a file?

like image 963
Heisenberg Avatar asked Mar 15 '26 04:03

Heisenberg


2 Answers

String Object is serialized at your client side and deserialized and is kept in the Heap memory. If you want it to be stored in your String Pool memory, you should use the intern() method.

    String value;
    String data = (value =getServiceData("clientKey"))==null?null:value.intern();
like image 121
Kumar Abhinav Avatar answered Mar 16 '26 19:03

Kumar Abhinav


Most methods which read strings from external sources (especially BufferedReader.getLine() or Java serialisation) will not intern the strings, so the answer is no.

However if you use third party libraries, they might do it: for example there are some XML/Dom parsers known to do that (at least for element names, less often for values). Also some high performance frameworks (servlet containers) to that for certain strings (for example HTTP header names).

But generally it is used very seldom in good(!) implementations as it is much less desirable as one might think. Don't forget: before you can intern a string it must exist as an object which needs to be collected anyway, so from the point of avoiding garbage using intern() does not help. It only might reduce the working set memory if those strings survive long time (which it is not in OLTP) and might speed up equality checks slightly. But typically this only helps if you do thousands of them on the same string object.

You can check yourself if the string is already interned (you should of course not do it in production code as it interns your string and it might not work in all implementations) with:

input == input.intern()?"yes":"no"`

And yes (as asked in a comment), having million instances of the same API key can happen with this. But don't be fooled to think this is a bad thing. Actually interning them would need to search for the value and deal with a growing string pool. This can take longer than processing (and freeing) the string. Especially when the JVM can optimize the string allocation with generational allocation and escape analysis.

BTW: Java 8u20 has a feature (-XX:+UseStringDeduplication -XX:+PrintStringDeduplicationStatistics) to detect duplicate strings in the background while doing the garbage collection in G1. It will combine those string arrays to reduce the memory consumption. (JEP192)

like image 26
4 revseckes Avatar answered Mar 16 '26 19:03

4 revseckes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!