JTextArea占用大量内存 [英] JTextArea consumes a lot of memory
问题描述
我在Java和Swing中遇到内存问题.我有一个JTextArea
(与JTextPane
相同的问题),可用于从C ++可执行文件重定向标准输出.而且由于我要输出很多标准输出,所以JTextPane
占用了大量空间.无论如何,我将其简化为以下代码,全部使用Java.
I having some memory issue with Java and Swing. I have a JTextArea
(same issue with JTextPane
) that I use to redirect stdout from an C++ executable. And because I'm outputting a lot of stdout, JTextPane
is consuming a lot of space. In any case, I boiled it down to the following code, all in Java.
private javax.swing.JTextArea jtextareastdout;
....
for (int i = 0; i < 200000; i++) {
String randomstr = UUID.randomUUID().toString();
jtextareastdout.setText(randomstr); //<tag_memory>
if (i % 100 == 0)
System.gc(); //<tag_no_help>
}
上面的代码消耗100MB.注释掉tag_memory行后,它要少得多(带有我所有其他代码和UI的30MB).如何减少Java的内存使用量?当前正在使用Java 7 Update 4.
The above code consumes 100MB. With tag_memory line commented out, a lot less (30MB with all my other code & UI). How can I reduce Java's memory usage? Currently using Java 7 update 4.
谢谢.
推荐答案
我只是不明白所有内存的去向.
I just don't understand where all the memory is going.
PlainDocument
讲一个故事:每个代码点一个或两个16位代码单元,一条线图开始,并且所有使其变为可编辑状态所需的阻抗.对于只读查看,我将使用重定向:yourprogram 2>&1 > view
.在Java中,您可以从stdin
读入List<String>
,每行一个String
,然后使用JTable
进行查看.默认的渲染器非常有效. 此处.
PlainDocument
tells the story: either one or two 16-bit code units per code point, a map of line starts and all the impedimenta needed to make it editable. For read-only viewing, I'd use redirection: yourprogram 2>&1 > view
. In Java, you could read from stdin
into a List<String>
, with one String
per line, and view it with a JTable
. The default renderer is quite efficient. There's a related example here.
这篇关于JTextArea占用大量内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!