JAXB 减小内存大小:XML 转对象

huangapple go评论73阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年10月14日 20:20:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/64353130.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定