Say I have a very large XML file to unmarshal, like 2 MB. I don't want to unmarshal the file and end up with (aprox.) 2 MB of objects in memory.
Instead, I want to unmarshal portions of the file on-demand (i.e. as I navigate through the object structure) and cleanup the references of the objects I have already read, making them available to the garbage collector.
Is it possible to tweak the JAXB implementation in a way it unmarshals the XML on-demand instead of entirely at once?
Take a look in this solution: Partial Unmarshalling of an XML using JAXB to skip some xmlElement
A specialist class was created to provide a mechanism to read the xml in parts.