String replace using huge heap space

java

Solution

Use `replace()` instead of `replaceAll()`. `replaceAll()` uses regular expression and you don't need them plus they are overhead.

Problem

I am doing a xml parsing and doing some string `replaceAll` which is using a huge amount of memory space as shown in the below image. Code goes like: ``` private final String getText() { // special handling for apostrophe encoding // site will expect both ' , ' and %27. // change %27 or 'or ' to ' return _text.toString().trim().replaceAll("'", "'") .replaceAll("'", "'").replaceAll("%27", "'"); } ``` The `getText()` method is frequently call from `endElement()` method of SAXParser. Can anyone suggest how do change this functionality which will use lesser heap space ![trace][1]

Original source