5

Strings are immutable. When I declare:

String a = "abc";
String a1 = "abc";

Both objects refer to same location. So how can I destroy this "abc" reference from String pool?

My use case is that I am developing a hardware application with less memory and for this, I need to clear the references from String pool to save memory.

Robin Rizvi
  • 5,113
  • 4
  • 27
  • 35
  • 3
    You can't (well, ultimately anything is possible with a lot of hacking, but there is no regular way to do this nor is it necessary to do this). – Jesper Jul 26 '16 at 08:32
  • 1
    you can't destroy, but you could create `new String("abc")` and will get a new reference to a new object in the heap – Andrew Tobilko Jul 26 '16 at 08:34
  • 11
    Sounds like an [XY problem](http://meta.stackexchange.com/a/66378/286538). What is the problem you are actually trying to solve? – RealSkeptic Jul 26 '16 at 08:34
  • Strings are immutable as you said, that means that there will not be any problems with both variables referencing the same object. That object will not change no matter what you do. – kalsowerus Jul 26 '16 at 08:35
  • or don't use literals and an `intern` method to prevent adding values to the pool – Andrew Tobilko Jul 26 '16 at 08:36
  • 2
    Related? http://stackoverflow.com/questions/5672688/can-we-avoid-interning-of-strings-in-java – Maroun Jul 26 '16 at 08:41
  • @MarounMaroun, and this is also related http://stackoverflow.com/questions/32526286/how-unreferred-values-from-string-pool-get-removed – Filosssof Jul 26 '16 at 08:46
  • I'm developing hardware application with less memory. For that purpose I need to clear the references to save memory. So its very important for me to remove that reference? Or you can tell me alternate option? – Rohit K. Sawai Jul 28 '16 at 09:33

1 Answers1

8

No, typically you can not "destroy reference from String pool in Java" manually.

The main reason I suppose why you are targeting it is to avoid out of memory errors. In Java 6 days all interned strings were stored in the PermGen – the fixed size part of heap mainly used for storing loaded classes and string pool. Besides explicitly interned strings, PermGen string pool also contained all literal strings earlier used in your program. The biggest issue with string pool in Java 6 was its location – the PermGen. PermGen has a fixed size and can not be expanded at runtime. You can set it using -XX:MaxPermSize=N option. This would lead to memory leaks and out of memory errors.

In Java 7 – the string pool was relocated to the heap. It means that you are no longer limited by a separate fixed size memory area. All strings are now located in the heap, as most of other ordinary objects.

You may also increase the string pool size by configuring -XX:StringTableSize=N. If you are not sure about the string pool usage, try -XX:+PrintStringTableStatistics JVM argument. It will print you the string pool usage when your program terminates.

In JDK, there is also a tool named jmap which can be used to find out number of interned strings in your application.

jmap -heap process_id

Eg:

jmap -heap 18974

Along with other output, this command also outputs number of interned strings and the space they occupy "xxxxxx interned Strings occupying xxxxxx bytes."

The rules for garbage collection of objects in the String pool are the same as for other Strings or any other object. But the fact that the String objects that correspond to String literals mostly are always reachable since there is an implicit reference to the string object in the code of every method that uses the literal and so typically they are not candidates for garbage collection. However, this is not always the case. If the literal was defined in a class that was dynamically loaded (e.g. using Class.forName(...)), then it is possible to arrange that the class is unloaded. If that happens, then the String object for the literal will be unreachable, and will be reclaimed when the heap containing the interned String gets GC'ed.

Robin Rizvi
  • 5,113
  • 4
  • 27
  • 35
  • Ok. But @Robin if there is no option so do you know any alternate way of handling Strings?(Not saying String object,handling set of characters) Look I'm developing software for hardware with very less memory and I need to handle large number of strings. So can you tell me any alternate way of saving memory without using String object? – Rohit K. Sawai Jul 28 '16 at 10:11
  • As per my understanding of your problem, would these approaches work: String x = new String("some_string"); // So "some_string" will get GC'ed eventually, if we remove references to it. or WeakReference r = new WeakReference(new String("some_string")); (though this might be risky) – Robin Rizvi Jul 28 '16 at 11:19
  • Ok. I'll try this one. Thank you. – Rohit K. Sawai Aug 01 '16 at 10:55
  • Accept as answer if it suits your question – Robin Rizvi Aug 19 '16 at 09:13