Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6890881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:20:18+00:00 2026-05-27T06:20:18+00:00

I am quite confused here. I am preparing to SCJP, I wrote a small

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:20:19+00:00Added an answer on May 27, 2026 at 6:20 am

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is getting me quite confused: I have a small application that uses a
I am quite confused... I am reading here and BasicClientCookie clearly implements Serializable per
Quite confused here. <html> <head> <style> .one { font-weight: normal; } .two { font-weight:
I am quite confused setting up cascade deletes in Doctrine 2. Here's what my
I'm quite confused. I'm using the animation frames to make the objects move. Here's
I am quite confused here. Today I came across this concept of AttributedString .
Here is WCF binding and security question I'm quite confused: You are hosting a
I am quite confused what I have done wrong in here. My PDO statement
I'm quite confused reading the doc of Ruby's system method here . I'm not
I'm really quite confused as to how I'm supposed to implement the following app.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.