We have a java/jruby webapp running under tomcat, and I have been analyzing the number of objects and memory use by the app during runtime. I have noticed after startup the class "org.jruby.RubyString" had 1,118,000 instances of the string "", the total amount of heap memory used by empty strings alone is 65mb, this to me is ridiculous because it is 15% of the memory used by the webapp. The empty string is only one example of many string values with this problem, if I can intern all the jruby strings I worked out I could save about 130mb.
I know in Java, each time when a string value is created, it will check if the value already exists in the string pool and reuse it if it does. I am wondering if there is an option in Jruby that has the same optimization? if so, how do I enable it?
Example in Jruby:
v1 = "a"
v2 = "a"
puts v1.object_id # => 3352
puts v2.object_id # => 3354
Example in Java:
String v1 = "a";
String v2 = "a";
System.out.println(v1.hashCode()); # => 97
System.out.println(v2.hashCode()); # => 97