---
title: "关于Java中String的Memory问题"
author: "mooncui"
date: "2007-10-11T10:48:02Z"
tags: []
slug: "java-string-memory"
draft: false
lastmod: "2007-10-11T10:48:02Z"
id: 130984
url: "https://mooncui.iteye.com/blog/130984"
---

昨天看到一个blog，用一个极端例子来说明Java中的内存问题：

[http://blog.xebia.com/2007/10/04/leaking-memory-in-java/](http://blog.xebia.com/2007/10/04/leaking-memory-in-java/)

测试代码如下：

01. public class TestGC {
02. private String large = new String(new char\[100000]);
03. public String getSubString() {
04. return this.large.substring(0,2);
05. }
06. public static void main(String\[] args) {
07. ArrayList&lt;string&gt;&lt;/string&gt; subStrings = new ArrayList&lt;string&gt;&lt;/string&gt;();
08. for (int i = 0; i &lt;1000000; i++) {
09. TestGC testGC = new TestGC();
10. subStrings.add(testGC.getSubString());
11. }
12. }
13. }

如果要改掉这个bug，只要改写一下getSubString的方法就好了。

1. public String getSubString() {
2. return new String(this.large.substring(0,2)); // &lt;-- fixes leak!
3. }

我测试了一下，确实如此。然后我看了一下JDK1.5.0\_06代码中String类

from JDK1.5.0\_06, String class:

01. public String substring(int beginIndex, int endIndex) {
02. if (beginIndex &lt; 0) {
03. throw new StringIndexOutOfBoundsException(beginIndex);
04. }
05. if (endIndex &gt; count) {
06. throw new StringIndexOutOfBoundsException(endIndex);
07. }
08. if (beginIndex &gt; endIndex) {
09. throw new StringIndexOutOfBoundsException(endIndex - beginIndex);
10. }
11. return ((beginIndex == 0) && (endIndex == count)) ? this :
12. new String(offset + beginIndex, endIndex - beginIndex, value);
13. }

这里用到的String的构造函数是private的

1. private String(int offset, int count, char value\[]) {
2. this.value = value;
3. this.offset = offset;
4. this.count = count;
5. }

上面那个例子程序改写的部分其实就是增加调用public的String构造函数另外创建一个String对象，我们再来看看这个public的构造函数，已经另外一个类似上面这个private的构造函数的public构造函数：

两个public的构造函数

01. public String(String original) {
02. int size = original.count;
03. char\[] originalValue = original.value;
04. char\[] v;
05. if (originalValue.length &gt; size) {
06. // The array representing the String is bigger than the new
07. // String itself.  Perhaps this constructor is being called
08. // in order to trim the baggage, so make a copy of the array.
09. v = new char\[size];
10. System.arraycopy(originalValue, original.offset, v, 0, size);
11. } else {
12. // The array representing the String is the same
13. // size as the String, so no point in making a copy.
14. v = originalValue;
15. }
16. this.offset = 0;
17. this.count = size;
18. this.value = v;
19. }
20. public String(char value\[], int offset, int count) {
21. if (offset &lt; 0) {
22. throw new StringIndexOutOfBoundsException(offset);
23. }
24. if (count &lt; 0) {
25. throw new StringIndexOutOfBoundsException(count);
26. }
27. // Note: offset or count might be near -1&gt;&gt;&gt;1.
28. if (offset &gt; value.length - count) {
29. throw new StringIndexOutOfBoundsException(offset + count);
30. }
31. char\[] v = new char\[count];
32. System.arraycopy(value, offset, v, 0, count);
33. this.offset = 0;
34. this.count = count;
35. this.value = v;
36. }

上面这两个public的构造函数，另外创建出来的String对象和原来的String对象并不共享其char数组的。而前面那个private的构造函数没有作arraycopy，是共享char其数组的。我想JDK这个实现有待商讨，内部使用的private的构造函数中不需要copy数组，可以共享数组。但private的构造函数被public的subString方法调用后，被外部使用时，就有问题了。

 以后写相关代码时要小心了！