How do I keep java XMLEconder from using so much memory when writing a List?

Skcussm

I have serializable relational javabean BO's in an ArrayList (boList) that I want to write to XML using XMLEncoder or some other standard method.

Using:

XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(
   new FileOutputStream("C:/tmp/test.xml")));

encoder.writeObject(boList);
encoder.close();

I run out of memory. If, however, I do this:

for(BO bo : boList)
{
    encoder.writeObject(bo);
    encoder.flush();
}
encoder.close();

It runs perfectly using very little memory. I know the flush() writes generated xml to the file and releases referenced memory which makes sense. I suspect when writing the boList the XMLEncoder it is trying to generate all XML before writing for which there is not enough memory.

I need to be able to write the boList to XML as it will be a field in a parent BO which will be written to XML. How do I get this to behave with the list as in the case where I call "writeObject" on each separate object? Is there an alternative to XMLEncoder that will accomplish this which is as straightforward to use? This must be accomplished using standard JRE/Java 6 functionality with no third party libraries. Thanks!

Skcussm

After a great deal of research, and reading of implementation code for the XMLEncoder, I have determined that it does indeed build all of the xml structure in memory before writing the output for each call to writeObject(...). There does not seem to be a feasible way around this using XMLEncoder.

I have decided to use an alternative third-party opensource library. XStream (http://x-stream.github.io/) provides exactly the same end functionality as XMLEncoder in this case, but is far more robust. It streams the generated xml to the destination as it is being generated resulting in a much reduced memory footprint.

In short, my answer is that XMLEncoder's memory footprint can not be reduced for large object graphs without actually changing XMLEncoder's implementation.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I avoid mapFailed() error when writing to large file on system with limited memory

How do I keep the iteration order of a List when using Collections.toMap() on a stream?

How do I keep Perl from consuming tons of memory when child forks of a large parent process shut down?

How do I keep JTextFields in a Java Swing BoxLayout from expanding?

How precisely do Java arrays use memory in HotSpot (i.e. how much slop)?

How can I find out how much memory a UIImageView is using?

Why does it consume so much memory when I multiply two CSR matrices using scipy?

how do i keep centered div with max-width from collapsing when using flexbox column?

How much performance do I loose when R is compiled with --enable-R-profiling and --enable-memory-profiling but not using both?

How do I list the values of an enum from a memory dump using SOS and Windbg?

How do I keep an onInput event from being triggered when using document.execCommand()?

Why is my browser using so much memory?

Why is Jenkins using so much memory?

I keep running out of memory and swapping but having a hard time figuring out what is using so much ram

How do I keep XmlDocument.Save from writing namespaces of new elements?

How do I find out which process is using so much of my RAM?

Why is mv so much faster than cp? How do I recover from an incorrect mv command?

Why is this code using so much memory?

Beginner Java attempt: am I using too much memory?

How do I avoid writing so much boilerplate for queries in ADO.NET on .NET 2.0?

how do I prevent my list items from being broken up into individual strings when writing to CSV?

How do I keep the logo from resizing when on mobile?

How do I keep a reference when using nested views in AngularJS?

Why do I need so much memory for WriterT State?

how do I make it so when I click a button from a foreach list, it only activates the button in that specific list?

How do I keep R from showing a dotted line in the legend when using partial transparency (alpha)?

how do i create a list so that when i append a variable the first elelmnt gets removed from list after a certin threshold

How do I get the ListIndex of an Item in a Combobox when I have the Value, so I can remove the Item from a ListBox with the same list?

How do I make it so that when I click it changes the list