java - FileInputStream and closing -


in code, wherever fileinputstream required argument, i'm doing this:

new obj(new fileinputstream(filelocation)); 

as not assigning fileinputstream reference variable, not closing using close() method.

does result in memory leaks?

aren't resources released fileinputstream in above approach?

if there no references input stream, garbage collected , stream automatically closed - see fileinputstream.finalize() method. however, not programming practice because not know when garbage collection happen. result in memory leak.

correct , safer approach use try resources block, or in older java versions try-catch-finally, close stream in block - ioutils apache commons, not have care ioexception thrown close() method


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -