使用StringBuilder的问题

发布于 2024-10-18 22:43:52 字数 735 浏览 1 评论 0原文

我正在研究一段代码的内存优化。它负责将图像文件传输到其他计算机。图像文件为 240Mb,当前应用程序堆大小为 1536Mb。

当前代码是

byte[] buf = new byte[size];

while ((num = is.read(buf)) > 0) {
...
String str = new String(buf, 0, num));
...
sendToPc(str);
}

这会创建很多字符串对象,当我尝试将图像推送到超过 5 台 PC 时,它会耗尽堆。所以我想到使用字符串生成器(我不关心同步)

但是字符串生成器没有像这样的变体

byte[] buf = new byte[size];
StringBuilder str = new StringBuilder(size); 
while ((num = is.read(buf)) > 0) { 
...
str.insert(0,buf); --> Apparently can not append byte array. 
...
str.delete(0, str.length());
}

即使我尝试 str.insert(0,new String(buf, 0, num)) 它没有任何区别。

任何想法,我如何压缩对象的数量,因为我不能使用字符串生成器或字符串缓冲区。

问候

迪拉吉·乔希

I am working on memory optimization of a piece of code. which is responsible for transferring an image file to other computer. Image file is 240Mb, and current app heap size is 1536Mb.

Current code is

byte[] buf = new byte[size];

while ((num = is.read(buf)) > 0) {
...
String str = new String(buf, 0, num));
...
sendToPc(str);
}

This creates a lot of string objects and when i try to push the image to more than 5 PC's it runs out of heap. So i thought of using string builder (I do not care about synchronization)

But string builder do not have a variant like

byte[] buf = new byte[size];
StringBuilder str = new StringBuilder(size); 
while ((num = is.read(buf)) > 0) { 
...
str.insert(0,buf); --> Apparently can not append byte array. 
...
str.delete(0, str.length());
}

And even if i try str.insert(0,new String(buf, 0, num)) it do not make any difference.

Any ideas, how can i squeeze the number of objects since i can not use string builder or string buffer.

Regards

Dheeraj Joshi

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我家小可爱 2024-10-25 22:43:52

您可能想尝试 IOUtils 从 < href="http://commons.apache.org/io/" rel="nofollow">apache 通用 io 库。它提供了复制大文件的方法。关于同一主题,《膨胀的原因,健康的极限》一文确实很有启发性。

You may want to try IOUtils from the apache common io library. It provides methods to copy large files. On the same topic, the paper "The Causes of Bloat, The Limits of Health" is really instructive.

养猫人 2024-10-25 22:43:52

您是否需要将 sendToPc 中的图像作为字符串发送?一些 ByteBuffer 可能会更适合您的需求......

Are you required to send the image in the sendToPc as a String? Some ByteBuffer will probabty suit better your needs ...

妄司 2024-10-25 22:43:52

您不应该使用字符串来保存二进制数据。字符串包含字符。当您以字节数组作为参数调用 String 构造函数时,您将根据平台的默认字符集构建一个字符串,该字符串将字节转换为字符。 javadoc 说

此构造函数的行为
给定的字节在中无效
默认字符集未指定

而且,发送平台的默认字符集不一定与接收平台的默认字符集相同。

为什么不简单地将字节作为字节数组传输?

You should not use Strings to hold binary data. Strings contain characters. When you invoke the String constructor taking a byte array as argument, you build a string based on the default charset of the platform, which transforms the bytes into chars. The javadoc says

The behavior of this constructor when
the given bytes are not valid in the
default charset is unspecified

Moreover, the default charset of the sending platform is not necessarily the same as the one of the receiving platform.

Why don't you simply transfer the bytes as a byte array?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文