[转译] Pay less for Strings

这是在eclipsepowered上看到的。 Ed提到了eclipse-dev committers mailing list中, Jerome Lanneluc 指出的一个String#substring和String(String)的问题。按照Jerome的说法,使用String#substring()的时候,原有的长字符串所占据的空间仍然被占据,并没有释放,使用String(String)可以解决这个问题。Jerome的原文:

I just noticed that String#substring(…) shares the underlying char array. So if you have a big string, take a substring of it, throw away the big string, you will still hold on the big char array. A simple way to solve this is to make a copy of the substring using the String(String) constructor.

I saved 550KB in JDT Core by changing the following code:
String simpleName =
fullyQualifiedName.substring(fullQualifiedName.lastIndexOf('.'));

to
String simpleName = new
String(fullyQualifiedName.substring(fullQualifiedName.lastIndexOf('.')));

Eclipse3.1发布以后,使用者发现,3.1占据了大量的Heap(大约200-300M)。 Eclipse的开发小组正在不断地解决这个问题,而String#substing的改进,将在Eclipse 3.1M6中发布(4月1日)。

这个提示,其实也可以运用在我们自己的项目之中

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值