2

I am quite confused here. I am preparing to SCJP, I wrote a small recursive to check BufferedWriter memory allocations before|after flush() & close() methods. Please see below code block, I have just wrote my Desktop files into a text file.

import java.io.*;

public class Myexception{

    private static String lvl = "";
    static BufferedWriter bw;
    private static File source = new File("C:\\Users\\"+System.getProperty("user.name")+"\\Desktop\\New folder\\myTest.txt");

    public static void main(String[] args) throws IOException {

        Runtime rt = Runtime.getRuntime();

        System.out.println("Free memory before recursive: " + rt.freeMemory());

        bw = new BufferedWriter(new FileWriter(source));

        checkFiles(new File("C:\\Users\\"+System.getProperty("user.name")+"\\Desktop"), 0);

        System.out.println("Memory after recursive: " + rt.freeMemory());

        bw.flush();
        bw.close();
        lvl = null;

        System.out.println("Memory after clean up: " + rt.freeMemory());
    }
    static void checkFiles(File file, int level) throws IOException{

        if(!file.exists()){

            System.out.println("File doesnt exist: " + file.getName() + file.getPath());
            return;
        }

        for(String s:file.list()){

            if(new File(file.getPath() + "\\" +  s).isDirectory()){

                bw.newLine();
                bw.write(lvl + "Directory: " + s);

                lvl += " ";

                checkFiles(new File(file.getPath() + "\\" +  s), level+1);

            }else{

                bw.newLine();
                bw.write(lvl + "File: " + s);

            }
        }
    }
}

Output is fine but what I didnt understand, free memory before than flush() & close() is the same as after than flush() & close().Please see my output:

Free memory before recursive: 126150232
Memory after recursive: 104461304
Memory after clean up: 104461304

I have checked existing topics but I couldn`t find exact explanation. I was expecting I will have more free memory after bw.close().

Thank you

elixenide
  • 44,308
  • 16
  • 74
  • 100
HRgiger
  • 2,750
  • 26
  • 37

2 Answers2

3

closing and flushing streams has nothing to do with "cleaning up memory", they make sure that data in a BufferedStream is flushed to whatever the stream is pointing to, for example the disk or network socket usually. Read the JavaDoc it doesn't ever imply that this has anything to do or effects garbage collection in anyway. You need to go back to studying before you waste your money on a worthless certification.

  • 2
    +1: flush and close ensure buffered data is written to the OS. – Peter Lawrey Dec 05 '11 at 13:40
  • @Jarrod Thank you for answer, I forget to mention that I am aware that they make sure buffered data is written into disk. As question topic I was just wondering if they effects also into memory. Because here http://www.coderanch.com/t/550351/Streams/java/Why-must-we-close-BufferedWriter I read some gc comments. +1 for explanation. – HRgiger Dec 05 '11 at 14:21
1

Memory is cleaned only after Garbage Collector has run. Until that you will not see any changes in memory consumption.

Nikem
  • 5,716
  • 3
  • 32
  • 59