1

I'm unclear as to whether creating a new File object in Java will actually store the data I write into the File onto storage, or whether it simply stores the File object on the JVM heap in memory.

If it's simply stored in memory, I was wondering how I can clear the File from memory once it's no longer needed to free up JVM heap space.

Thank you!

Avi
  • 87
  • 1
  • 5
  • Java will store a file object in memory for as long as it is in scope. Then it will get put up for garbage collection. When Java writes to the file, several buffers may be created, at both Java level and more so at the OS level. As far as I am aware JAVA will not store the whole file in memory, unless of course, you read the whole contents into an object you have created – jr593 Sep 27 '19 at 09:13
  • TL;DR: No, `File` is essentially just a path, and you don't need to worry about it. – Kayaman Sep 27 '19 at 09:13
  • A `File` is "_an abstract representation of file and directory pathnames_". The object does not open any resources and does not hold the data of the underlying file, if it even exists, in memory. As for freeing up heap space when the `File` object is no longer needed, you do what you would do for any other object you no longer need: Remove all strong references and let the garbage collector take care of it. – Slaw Sep 27 '19 at 09:16
  • Yes, of course, objects are not zero size. There is no such thing as 'the data I write into the File', and your sentence contains in contains a false apposition. – user207421 Sep 27 '19 at 09:36

0 Answers0