且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

有多少人被 Java 子字符串内存问题所困扰?

更新时间:2021-09-15 15:58:08

我被它咬了一口,逐行阅读字典文件.每行都很短,但是 BufferedReader 创建的缓冲区意味着每个字符串都由一个 80 字符的数组支持.

I've been bitten by it once, reading a dictionary file line by line. Each line was very short, but the buffer created by BufferedReader meant that each string was backed by an 80-char array.

那是我第一次了解到写作的要点:

That was when I first learned the point of writing:

word = new String(word);

虽然大多数时候这不是问题 - 当然它比采取完全独立的副本"方法更有效.

Most of the time it's not a problem though - and of course it can be more efficient than the "take a completely separate copy" approach.