JAXB UnMarshaller内存消耗

时间:2015-11-25 16:26:40

标签: groovy gradle jaxb unmarshalling

我已经通过' xjc'从XSD创建了几个课程。命令,现在我在Gradle / Groovy中通过XMLStreamReader和JAXB(它通过块读取块)来解析大型XML文件:

def getMemory(){
        long used  = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
        return used / (1024*1024)
    }

def xmlUnMarshaller(FileReader fileReader) {
        System.out.println("Memory Before:${getMemory()}")

        XMLInputFactory xif = XMLInputFactory.newFactory()
        XMLStreamReader xsr = xif.createXMLStreamReader(fileReader);
        def jContext = (JAXBContext) JAXBContext.newInstance(Patient.class)
        Unmarshaller unmarshaller = jContext.createUnmarshaller()
        def patientCount = 0
        while (xsr.hasNext()) {
            if (xsr.isStartElement() && xsr.getLocalName().equals("patient")) {
                def patient = (Subject) unmarshaller.unmarshal(xsr)
                patientCount++
            } else {
                xsr.next()
            }
        }
        xsr.close()

        System.out.println("Memory After:${getMemory()}")
    }

在' jaxb.properties'文件,我已提到:

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

工作正常 但是使用大量内存!这是运行jar文件后的内存状态:

Memory Before:19.5550079345703125
Memory After :98.85445404052734375

我已经使用Gradle来构建jar文件。令人惊讶的是,如果我在Intellij中调试相同的代码,它会显示不同的内存使用情况(我认为这是合理的):

Memory Before:45.2733917236328125
Memory After :37.20656585693359375

这些是我在build.gralde上使用的依赖项:

dependencies {
    compile 'org.codehaus.groovy:groovy-all:2.3.1'
    compile 'org.eclipse.persistence:org.eclipse.persistence.moxy:2.6.1'
    jaxb 'com.sun.xml.bind:jaxb-xjc:2.2.7-b41'
    jaxb 'com.sun.xml.bind:jaxb-impl:2.2.7-b41'
    jaxb 'javax.xml.bind:jaxb-api:2.2.7'
}

为什么会这样?为什么运行jar文件时会占用这么多内存?

0 个答案:

没有答案