是在Oracle官网看到的。网址不记得了。

public class TestGC {
        private String largeString = new String(new byte[100000]);
        private String smallString = "foo";
        
        String getString() {
                // if caller stores this substring, this    customer    will not be gc'ed
                //return this.largeString.substring(0,2);
                return new String(this.largeString.substring(0,2)); // no error here!
//                return smallString; // no error here!
        }
        
        public static void main(String[] args) {
                java.util.ArrayList list = new java.util.ArrayList();
                for (int i = 0; i < 1000000; i++) {
                        TestGC gc = new TestGC();
                        list.add(gc.getString());
                }
        }
}