我一直在这个问题上打我的头一段时间。我采用 27K 编码字符串(类似于 URL 编码)并将其转换回 9K“ISO-8859-1”纯文本字符串。
byte outarray[] = new byte[decoded_msg_length]; // 9K
byte inarray[];
try {
inarray = instring.getBytes("ISO-8859-1"); // eg: "ÀÀÀÚßÐÀÀÃÐéÙÓåäàÈÂÁÙÈ...."
inarray = null; // free up whatever memory possible.
// ... for loop decodes chunks of 4 bytes...
Runtime runtime = Runtime.getRuntime();
System.out.println("freeMemory1="+runtime.freeMemory()); // freeMemory1=86441120
// yes I've tried methods like new String( outarray, "ISO-8859-1" );, etc.
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
byteStream.write(outarray);
outarray=null;
runtime.gc();
System.out.println("freeMemory2="+runtime.freeMemory()); //freeMemory2=133761568
// return new String(outarray,"ISO-8859-1"); // OutOfMemoryException thrown here
// return new String(outarray); // OutOfMemoryException thrown here too
return byteStream.toString("ISO-8859-1"); // OutOfMemoryException thrown here also
// sample output: "JOHN H SMITH 123 OAK ST..."
} catch( IOException ioe ) {
...
}
// Thrown exception:
Exception in thread "main" java.lang.OutOfMemoryError
at java.lang.StringCoding.decode(StringCoding.java:510)
at java.lang.String.<init>(String.java:232)
at java.io.ByteArrayOutputStream.toString(ByteArrayOutputStream.java:195)
...
看来我的记忆力很足。相同的代码在 Windows 中的可用内存不到一半的情况下运行良好。我将它作为一个独立的类运行。任何人都知道任何与 JRE 内存泄漏有关的 Linux 编码问题吗?
$ java -version
java version "1.5.0"
Java(TM) 2 Runtime Environment, Standard Edition (build pxi32dev-20080315 (SR7))
IBM J9 VM (build 2.3, J2RE 1.5.0 IBM J9 2.3 Linux x86-32 j9vmxi3223-20080315 (JIT enabled)
J9VM - 20080314_17962_lHdSMr
JIT - 20080130_0718ifx2_r8
GC - 200802_08)
JCL - 20080314