0

I currently have a giant method returning a string based on a given integer. It uses a switch statement with 686 cases, each case returning a string constant. There are 683 unique strings of lengths ranging from 10 to 50 characters approximately, all used inside that method only. I would like to know when these strings get loaded. Do they get loaded at class load time? If so, is there a way I could load these strings only when I first need them, and get garbaged when there are no more references?

For now, I am currently loading those strings from a file containing a table of strings. They are to be placed in a WeakHashMap. Is there a better way of doing these?

Another alternative I am thinking of is to make a class holding each string in a static field. That way, it will only be loaded the first time I fetch the field's value. But that would make my package huge.

Also, does dalvik-vm uses the same string constant loading routine.

Jason Sparc
  • 702
  • 2
  • 7
  • 29
  • 2
    2 bytes/char * 50 chars/string * 700 strings = 70 kB of data. I wouldn't worry about that. – John Dvorak Dec 13 '13 at 06:53
  • 1
    You should really be using an associative array for that, not a switch statement. I would hate to see that switch statement. You could then put the associative array at whatever level you want and pull what you need from it where you want. – Leifingson Dec 13 '13 at 06:55
  • This has your answer: http://stackoverflow.com/questions/4918399/what-type-of-memory-heap-or-stack-string-constant-pool-in-java-gets-stored – Paul Draper Dec 13 '13 at 06:55
  • 1
    @Leifingson, perhaps you could explain "associative array" to something most Java programmers would recognize, e.g. `java.util.Map` – Paul Draper Dec 13 '13 at 06:56
  • @PaulDraper isn't that a duplicate, then? – John Dvorak Dec 13 '13 at 06:56
  • @PaulDraper added my vote. Let's see if others agree. – John Dvorak Dec 13 '13 at 06:59
  • If I use a map, I would still need to load the strings from somewhere else. Actually, my first implementation uses WeakHashMap – Jason Sparc Dec 13 '13 at 07:02

1 Answers1

1

Sun made an optimization that is rather confusing to many new Java programmers called the String Pool. It allows for Strings, which are one of the most used Objects to optimize themselves and save space. An important point to make is that the String Pool only applies to String literals, meaning their value was assigned, not constructed using a String constructor.

String pooling (sometimes also called as string canonicalisation) is a process of replacing several String objects with equal value but different identity with a single shared String object. You can achieve this goal by keeping your own Map (with possibly soft or weak references depending on your requirements) and using map values as canonicalised values. Or you can use String.intern() method which is provided to you by JDK.

All strings in the JVM string pool are eligible for garbage collection if there are no references to them from your program roots. It means that if your interned string went out of scope and there are no other references to it – it will be garbage collected from the JVM string pool.

So you need not to bother about it, as JVM already doing it internally.