When will the new String() object in memory gets cleared after invoking intern() method
问题 List<String> list = new ArrayList<>(); for (int i = 0; i < 1000; i++) { StringBuilder sb = new StringBuilder(); String string = sb.toString(); string = string.intern() list.add(string); } In the above sample, after invoking string.intern() method, when will the 1000 objects created in heap (sb.toString) be cleared? Edit 1: If there is no guarantee that these objects could be cleared. Assuming that GC haven't run, is it obsolete to use string.intern() itself? (In terms of the memory usage?) Is