String中substring方法内存泄漏问题

众所周知,JDK中以前String类中的substring方法存在内存泄漏问题,之所以说是以前,是因为JDK1.7及以后的版本已经修复了,我看都说JDK1.6的版本也存在这个问题,但是我本机上安装的1.6看了看源码不存在内存泄漏问题啊,又看了1.7的源码,和我本机的1.6的一样,是不是我的1.6版版其实是1.7的?!唉,不管了,反正1.7版本肯定没有这个问题(1.5及更老版本肯定有)了,大家就放心的用吧。

之所以存在内存泄漏的问题,是因为原先的版本中,substring是这样实现的:

   public String substring(int beginIndex, int endIndex) {
	if (beginIndex < 0) {
	    throw new StringIndexOutOfBoundsException(beginIndex);
	}
	if (endIndex > count) {
	    throw new StringIndexOutOfBoundsException(endIndex);
	}
	if (beginIndex > endIndex) {
	    throw new StringIndexOutOfBoundsException(endIndex - beginIndex);
	}
	return ((beginIndex == 0) && (endIndex == count)) ? this :
	    new String(offset + beginIndex, endIndex - beginIndex, value);
    }

而其中用到的String构造方法是这样的:

    // Package private constructor which shares value array for speed.
    String(int offset, int count, char value[]) {
	this.value = value;
	this.offset = offset;
	this.count = count;
    }

this.value=value这种实现就出现问题了,因为String类中有几个私有的成员变量:

 /** The value is used for character storage. */
    private final char value[];

    /** The offset is the first index of the storage that is used. */
    private final int offset;

    /** The count is the number of characters in the String. */
    private final int count;

    /** Cache the hash code for the string */
    private int hash; // Default to 0

明白了吧,这种实现还在引用着原先字符串变量的value[],通过offset和count返回一个长得像的“截取”后的字符串给人一种错觉,导致JVM认为这个最初字符串还在被引用着不对其gc,不过之所以这么做SUN公司(oracle 10年收购了)的JDK编写人员也是有原因的,就是效率问题,如注释所说:

Packageprivateconstructorwhichsharesvaluearrayforspeed.

这样导致的后果就是如果有一个很大很长的字符串我只需要其中的一小部分字符串用substring实现的话,如果让你看似得到的“新”的短小字符串一直没被JVM回收的话,那么相当这个最初的大字符串也没被回收,尤其是你把这个短小“新”的字符串直接以引用的形式付给一个静态的全局变量,在加上如果访问数量很大,那应该“代价”还是蛮可观的,不过可以简单的这样new(s.substring())就避免了这个问题。

新的JDK中substring之所以不存在这个问题了,是因为这个构造方法改成这样了:

  public String(char value[], int offset, int count) {
        if (offset < 0) {
            throw new StringIndexOutOfBoundsException(offset);
        }
        if (count < 0) {
            throw new StringIndexOutOfBoundsException(count);
        }
        // Note: offset or count might be near -1>>>1.
        if (offset > value.length - count) {
            throw new StringIndexOutOfBoundsException(offset + count);
        }
        this.offset = 0;
        this.count = count;
        this.value = Arrays.copyOfRange(value, offset, offset+count);
    }

其中 value变量不再引用了而是重新新建了一个,所以没有这个问题了,是不是大家看完之后对这个方法有了更全新的认识?大笑

转载请注明—作者:Java我人生(陈磊兴)原文出处:http://blog.csdn.net/chenleixing/article/details/43646255


最后,认真看过的网友们,大神们,如有感觉我这个程序猿有哪个地方说的不对或者不妥或者你有很好的

议或者建议或点子方法,还望您大恩大德施舍n秒的时间留下你的宝贵文字(留言),以便你,我,还有广大的程序猿们更快地成长与进步.......



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Arduinostring(字符串)是一种表示文本数据的数据类型,也是一个标准库类型,可以通过包含<string.h>头文件来使用。Arduinostring类型与C++标准库string类型非常相似,可以进行各种操作,例如: - 求长度:使用length()或size()函数获取字符串的长度。 - 拼接:使用+运算符将两个字符串拼接起来。 - 截取:使用substring()函数截取字符串的一部分。 - 查找:使用indexOf()函数查找某个字符或子串在字符串的位置。 - 替换:使用replace()函数替换字符串的某个字符或子串。 例如,以下代码演示了如何对字符串进行一些常见操作: ```arduino #include <string.h> String str1 = "Hello"; String str2 = "World"; // 拼接字符串 String str3 = str1 + " " + str2; Serial.println(str3); // 输出:Hello World // 求字符串长度 Serial.println(str3.length()); // 输出:11 // 截取子串 String str4 = str3.substring(6, 11); // 截取从第6个字符开始的5个字符 Serial.println(str4); // 输出:World // 查找子串 int pos = str3.indexOf("World"); if (pos >= 0) { Serial.println("Found at position " + String(pos)); // 输出:Found at position 6 } // 替换子串 str3.replace(6, 5, "Universe"); // 将World替换为Universe Serial.println(str3); // 输出:Hello Universe ``` 需要注意的是,在Arduino使用string类型时,需要使用String类(注意大小写)来定义和操作字符串变量,而不是使用标准库string类型。另外,由于Arduino的内存资源有限,字符串操作时需要注意避免内存泄漏问题

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值