简体   繁体   English

如何避免用StringBuilder或Java中的String占用内存

[英]How to avoid out of memory in StringBuilder or String in Java

I am getting a lot of data from a webservice containing xml entity references. 我从包含xml实体引用的Web服务中获取了大量数据。 While replacing those with the respective characters I am getting an out of memory error. 在用相应的字符替换那些字符时,我遇到了内存不足的错误。 Can anybody give an example of how to avoid that? 有人可以举一个例子来避免这种情况吗? I have been stuck for two days on this problem. 我已经在这个问题上停留了两天。

This is my code: 这是我的代码:

public  String decodeXMLData(String s)
 {
     s = s.replaceAll(">",">");
     System.out.println("string value is"+s);

     s = s.replaceAll("&lt;", "<"); 
     System.out.println("string value1 is"+s);
     s = s.replaceAll("&amp;", "&");

     s = s.replaceAll("&quot;", "\"");

      s = s.replaceAll("&apos;", "'");

      s = s.replaceAll("&nbsp;", " ");

     return s;
 } 

Calling five times replaceAll, you are creating five new String objects. 调用五次replaceAll,将创建五个新的String对象。 In total, you are working with six Strings. 总共要使用六个String。 This is not an efficent way to XML-decode a string. 这不是XML解码字符串的有效方法。

I reccommend you using a more robust implementation of XML-encoding/decoding methods, like those contained in Commons Lang libraries. 我建议您使用XML编码/解码方法的更可靠实现,例如Commons Lang库中包含的方法。 In particular, StringEscapeUtils may help you to get your job done. 特别是, StringEscapeUtils可以帮助您完成工作。

The method as shown would not be a source of out of memory errors (unless the string you are handling is as big as the remaining free heap). 所示方法不会导致内存不足错误(除非您正在处理的字符串与剩余的可用堆一样大)。

What uou could be running into is the fact that String.substring() calls do not allocate a new string, but create a string object which re-uses the one that substring is called on. uou可能遇到的事实是String.substring()调用没有分配新字符串,而是创建了一个字符串对象,该对象重新使用了调用子字符串的对象。 If your code exists of reading large buffers and creating strings from those buffers, you might need to use new String(str.substring(index)) to force reallocation of the string values into new small char arrays. 如果您的代码存在读取大型缓冲区并从这些缓冲区创建字符串的情况,则可能需要使用new String(str.substring(index))强制将字符串值重新分配到新的小型char数组中。

You can try increasing JVM memory, but that will only delay the inevitable if the problem is serious (ie if you're trying to claim gigabytes for example). 您可以尝试增加JVM内存,但这只会在问题很严重的情况下(例如,如果您要声明千兆字节)延迟不可避免的事件。

If you've got a single String that causes you to run out of memory trying to do this, it must be humongous :) Suggestion to use a SAX parser to handle it and print it in bits and pieces is a good one. 如果只有一个String导致尝试执行此操作会耗尽内存,那么它一定是巨大的:)建议使用SAX解析器来处理它并一点一点地打印它是一个好方法。 Or split it up into smaller bits yourself and send each of those to a routine that does what you want and discard the result afterwards. 或自己将其拆分为较小的部分,然后将每个部分发送到执行所需操作的例程中,然后丢弃结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM