0

Will this line

bufferedReader.readLine();

Without saving it into a variable will consume memory?

3
  • 1
    Yes, of course. Have you read the documentation of BufferedReader? What's not clear about it? And whatever it does, it still has to read and create a String an return it, and that consumes memory. Why do you care. Memory is meant to be used. Commented Nov 23, 2014 at 8:42
  • @JBNizet I know that readline() returns String!, the question is if a return value does not "saved" will it consume memory or will automateclly "burn" by the JVM Commented Nov 23, 2014 at 8:47
  • It will be garbage-collected by the JVM at some time, as every object that is not reachable anymore. But that's not what you asked. Commented Nov 23, 2014 at 8:51

2 Answers 2

2

Yes, it will consume memory because it at the very least it has to allocate memory for the result (even if you don't assign it).

Probably, the memory will be garbage collected fairly soon because no one is holding a reference to it. But there's no guarantees when the garbage collector will be run. And to answer the question is doesn't matter anyway because it isn't about the time frame the memory is allocated.

Please also note that it will definitely not be "optimised away" because it triggers a side effect which has to be performed even if the result is discarded immediately. The compiler&JVM "know" that and therefore from a memory point of view this is pretty much just like assigning the result and then immediately overwriting the result with something else.

Sign up to request clarification or add additional context in comments.

Comments

1

As far as i know, it would be available for Garbage Collection immediately, because it doesn't has any references attached to it.

2 Comments

But meanwhile it will consume whatever memory is required while it is executing.
Yes i think it will :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.