Heap issues using XStream

858 Views Asked by At

I am getting OutOfMemoryError using XStream while converting Java to XML and writing to a file. Its happening at xstream.toXMl operation. I am able to write around 200 MB to the file with warnings in console saying that 700 mb heap used etc.. but after that, I get the error and the processing stops.

I am using XStream version 1.4.7 and Java 1.7.

Below is the code:

 final File xmlFile =
            new File(System.getProperty("user.dir") + "\\" + "Test.xml");
        final Writer out =
            new BufferedWriter(new OutputStreamWriter(new FileOutputStream(xmlFile),
                    "UTF-8"));
        try {
            final Map values =  data from gemfire cache
            xstream.toXML(values, out);
            // final ObjectOutputStream outStream = xstream.createObjectOutputStream(out);
            // outStream.writeObject(values);
        } finally {
            out.close();
        }

I tried doing the below to get rid of the error but did not work.

1).I tried adding XPP3 (from org.ogce) 1.1.6 and XMLPULL (from xmlpull) 1.1.3.4a maven dependencies 2). Did final XStream xstream = new XStream(new StaxDriver());

3). Changed xms and xmx in eclipse.ini to 1024m. Here's what my eclipse.ini looks like

-startup
 plugins/org.eclipse.equinox.launcher_1.3.0.v20130327-1440.jar
 --launcher.library
 plugins/org.eclipse.equinox.launcher.win32.win32.x86_64_1.1.200.v20130807-1835
-vm
 C:/Program Files/Java/jdk1.7.0_51/bin/javaw.exe
-product
 org.eclipse.epp.package.standard.product
--launcher.defaultAction
 openFile
--launcher.XXMaxPermSize
 1024M
 -showsplash
 org.eclipse.platform
--launcher.XXMaxPermSize
 1024m
--launcher.defaultAction
 openFile
 --launcher.appendVmargs
 -vmargs
 -Dosgi.requiredJavaVersion=1.6
 -Xms1024m
 -Xmx1024m
 -Dorg.eclipse.swt.browser.IEVersion=10001
 -Dsubversion.native.library=C:/Program Files/SlikSvn/bin/libsvnjavahl-1.dll

4). Tried to use ObjectStream as you can see in the code above commented

5). Added run time arguments Xms1024m -Xss256k -Dinsight.enabled=false -XX:PermSize=512M -XX:MaxPermSize=512m

6). Added run time argument - -XX:+UseConcMarkSweepGC

Please advise..

1

There are 1 best solutions below

0
On

I have also realized this issue, when i was serializing Java object into Xml. Here root cause is: Your java object may consists instance variable that is shared variable like single ton object etc. For such instance variable, you need to initialize variable such a way, it will use not use single ton object. You can create new object in within class and assign to your instance variable, So when serializing such instance variable, it will not create any issue.

I have fixed my issue by doing same where previously getting same error.

If you need further help, let me know.