Details
-
Bug
-
Status: Closed
-
Major
-
Resolution: Fixed
-
1.8.0, 1.9-beta-3
-
Patch
Description
public GPathResult parse(final File file) throws IOException, SAXException { final InputSource input = new InputSource(new FileInputStream(file)); input.setSystemId("file://" + file.getAbsolutePath()); return parse(input); }
I stepped through the SAX code but couldn't see the InputSource being closed so XmlSlurper is leaking file descriptors.
I think the Reader and InputStream methods' JavaDoc should also mention that the streams aren't closed.
GPathResult parse(final String uri) also seems to leak though that could also be a Xerces issue because I don't see it closing its input streams, either.
Shameless plug: Use Resource.close
I tried to get the VM to crash. After checking ulimit -a, I ran
5000.times { new XmlSlurper().parse(new File('web.xml')) }
This caused a lot of FileInputStreams to be created but I couldn't reach my limit of 1024 due to the finalizer in FileInputStream. Still, in the screenshot below you can see how the FileInputStream instances are accumulating on the heap.
Fixing this is of course easy:
public GPathResult parse(final File file) throws IOException, SAXException { FileInputStream in = null; try { in = new FileInputStream(file); final InputSource input = new InputSource(in); input.setSystemId("file://" + file.getAbsolutePath()); return parse(input); } finally { close(in); // or if (in != null) try { in.close() } catch (IOException ex) {} } }