gpt4 book ai didi

java - JDOM需要多少 "overhead"内存来生成XML文件?

转载 作者:太空宇宙 更新时间:2023-11-04 08:50:47 26 4
gpt4 key购买 nike

我需要使用 JDOM 生成 XML 文件,该文件可能相当大。我想知道除了内存中已经存在的数据(主要是字符串)之外,JDOM 还需要多少额外的内存空间。我写了一个简单的程序来测试,结果发现开销大约是XML内容的两倍。

有人知道为什么 JDOM 需要那么多额外的内存吗?是否有办法优化它? JDOM 对象不应该只保留对现有字符串的引用吗?

这是我用来测试的程序:

public class TestJdomMemoryOverhead {
private static Runtime runtime = Runtime.getRuntime();

public static void gc() {
// Try to give the JVM some hints to run garbage collection
for (int i = 0; i < 5; i++) {
runtime.runFinalization();
runtime.gc();
Thread.currentThread().yield();
}
}

public static void generateXml(List<String> filenames) throws IOException {
// generate a simple XML file by these file names. It looks like:
// <?xml version="1.0" encoding="UTF-8"?>
// <files>
// <f n="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" />
// <f n="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" />
// <f n="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" />
// ....
// ....
// </files>
Element filesElem = new Element("files");
Document doc = new Document(filesElem);
for (String name : filenames) {
Element fileElem = new Element("f");
fileElem.setAttribute("n", name);
filesElem.addContent(fileElem);
}
gc();
System.out.println("After generating JDOM objects: " + (runtime.totalMemory() - runtime.freeMemory()) + " bytes");
XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
BufferedWriter writer = new BufferedWriter(new FileWriter("test.xml", false));
outputter.output(doc, writer);
writer.close();
gc();
System.out.println("After writing to XML file: " + (runtime.totalMemory() - runtime.freeMemory()) + " bytes");
}

public static void main(String[] cmdArgs) throws IOException {
List<String> filenames = new ArrayList<String>();
StringBuilder builder = new StringBuilder();
// 30 unicode chracters, repated 500,000 times. The memory to store
// these file name strings should be about 30MB.
builder.append("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
for (int i = 0; i < 500000; i++) {
filenames.add(builder.toString());
}
gc();
System.out.println("After generating file names: " + (runtime.totalMemory() - runtime.freeMemory()) + " bytes");
generateXml(filenames);
gc();
System.out.println("Get back to main: " + (runtime.totalMemory() - runtime.freeMemory()) + " bytes");
}
}

输出为:

After generating file names: 51941096 bytes
After generating JDOM objects: 125766824 bytes
After writing to XML file: 126036768 bytes
Get back to main: 51087440 bytes

如您所见,JDOM 对象使用了大约 70MB。

最佳答案

JDOM 需要如此多内存的原因是因为 JDOM 主要是像 DOM 一样基于树的 API(文档树是在内存中创建的,就像您使用它的方式一样。)。但它比 DOM 性能更高。如果您要创建大型 XML 文档,您可能需要考虑使用类似 XMLStreamWriter 的内容。与jdk6捆绑在一起

这是一个简短的article JDOM 无法做到的事情

关于java - JDOM需要多少 "overhead"内存来生成XML文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3384845/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com