英文:
JAXB reduce memory size XML to Object
问题
任何减少JAXB中XML内存大小的建议。
XML文件大小 - 1 MB
在JAXB中Unmarshall后堆中使用的内存 - 7 MB
System.out.println("heapFreeSize--->"+Runtime.getRuntime().freeMemory());
JAXBContext jaxbContext = JAXBContext.newInstance(Document.class);
Unmarshaller unmarshallerJaxb = jaxbContext.createUnmarshaller();
Document document = (Document) unmarshallerJaxb.unmarshal(new File("test.xml"));
System.out.println("heapFreeSize--->"+Runtime.getRuntime().freeMemory());
我们的XSD中有大约100个属性,都是字符串。是否有更好的优化方法来减少内存,因为我们有大量的多个xml文件。
英文:
Any advise of reducing memory size of XML in JAXB.
XML File Size - 1 MB
Memory used in Heap after Unmarshall in Jaxb - 7 MB
System.out.println("heapFreeSize--->"+Runtime.getRuntime().freeMemory());
JAXBContext jaxbContext = JAXBContext.newInstance(Document.class);
Unmarshaller unmarshallerJaxb = jaxbContext.createUnmarshaller();
Document document = (Document) unmarshallerJaxb.unmarshal(new File("test.xml"));
System.out.println("heapFreeSize--->"+Runtime.getRuntime().freeMemory());
We have around 100 attributes in XSD and all are string. Any better optimization to reduce memory as we have huge volume of multiple xmls.
答案1
得分: 4
我认为以那种方式测量内存使用效率不高,因为在初始化JAXB上下文和执行取消编组时会实例化许多类。大部分已使用的内存将在下次垃圾回收运行时被释放。
要检查特定对象的内存使用情况,您可以使用java.lang.instrument包,如此答案所示。
此外,您还可以使用JDK中提供的jconsole在运行时观察应用程序的内存使用情况。它应该显示在对象使用时使用了多少内存,以及在不再使用这些对象后是否被释放。
通常情况下,JAXB是相当高效的,除非您的应用程序处理非常大的XML文件,否则您不需要关心内存问题。
英文:
I don't think that measuring memory usage in that way would be efficient, because there are lot of classes instantiated when initializing JAXB context and performing unmarshalling. Most of the used memory will be freed by GC, when it runs next time.
For checking memory usage of a particular object you could use java.lang.instrument package, as shown in this answer.
Also you can observe memory usage of your application in runtime using jconsole, which is available in JDK. It should show how much memory is used while object are in use and whether it's freed after they are not used anymore.
Generally JAXB is quite efficient and you shouldn't care about memory issues unless your application handles XMLs of very large size.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论