In some case, I need to operator Doc file in Java, I usually need new a File instance, like:
File file = new File("path/to/target/file");
should I do somethine after I use it for release memory?
file = null;
seems not work well.
In some case, I need to operator Doc file in Java, I usually need new a File instance, like:
File file = new File("path/to/target/file");
should I do somethine after I use it for release memory?
file = null;
seems not work well.
file = null;
[probably] won't do anything as long as the stream or reader you used to open the file still exists because that stream or reader [probably] will hold a reference to that File
. And anyway, a File
is virtually nothing in the bigger picture of memory consumption; it's highly unlikely your program will live or die over the continued existence of a single File
object.
That said, there's certainly no harm in doing file = null;
when you have no further use for that File
instance. Just don't be either surprised or worried if the available memory doesn't instantly jump up by a few bytes!
First, a File
instance is not much more than the file name. Its memory footprint is negligible.
Java has a garbage collector that will take care of reclaiming unused (unreachable) objects. There is no special action necessary on your side.