Real Life, Practical Example of Using String.intern() in Java?

后端 未结 5 1121
挽巷
挽巷 2021-01-01 11:47

I\'ve seen many primitive examples describing how String intern()\'ing works, but I have yet to see a real-life use-case that would benefit from it.

The only situati

5条回答
  •  北海茫月
    2021-01-01 12:10

    Interning can be very beneficial if you have N strings that can take only K different values, where N far exceeds K. Now, instead of storing N strings in memory, you will only be storing up to K.

    For example, you may have an ID type which consists of 5 digits. Thus, there can only be 10^5 different values. Suppose you're now parsing a large document that has many references/cross references to ID values. Let's say this document have 10^9 references total (obviously some references are repeated in other parts of the documents).

    So N = 10^9 and K = 10^5 in this case. If you are not interning the strings, you will be storing 10^9 strings in memory, where lots of those strings are equals (by Pigeonhole Principle). If you intern() the ID string you get when you're parsing the document, and you don't keep any reference to the uninterned strings you read from the document (so they can be garbage collected), then you will never need to store more than 10^5 strings in memory.

提交回复
热议问题