Issue
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.
Solution
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.
Answered By - Alexandra Dudkina
Answer Checked By - Clifford M. (JavaFixing Volunteer)