Wednesday, September 24, 2014

JAVA: Be careful with String class – Performance and Memory

 

1. Operation such as

- String.split()

- String.substring()

uses offset values but keep the ORIGINAL char[] value. Therefore, if you only ever want a portion of the String it will WASTE memory space by storing the original string value

http://stackoverflow.com/questions/7629208/substrings-and-garbage-in-java-1-6

2.  For String.substring()

to get just the portion of the substring, recommend to use

String sub = new String(str.substring(6,12));

http://stackoverflow.com/questions/1281549/memory-leak-traps-in-the-java-standard-api/1281569#1281569


3. General String related:


http://www.javamex.com/tutorials/memory/string_memory_usage.shtml

No comments:

Post a Comment