Wednesday, July 29, 2009

String.substring is dangerous

String.substring() will create a new string and old one can not be garbage collected. Please use below.

     String sub = new String( oldString.substring(0, 4) );

1 comment:

  1. But still you can refer to oldString using the name oldString...

    oldString will still not be eligible for garbage collection, unless you make it null explicitly.

    ReplyDelete