2

Using Java IO streams, it is quite often we use objects solely as constructors for other objects. I am interested in the memory implications of this prospect. For example, how does memory allocation differ in these two statements that do the same thing?

FileInputStream inputFile = new FileInputStream("filepath");
Scanner inStream = new Scanner(inputFile);

and

Scanner inStream = new Scanner(new FileInputStream("filepath"));
3
  • 4
    It's really implementation-specific, but it's certainly not going to be a huge difference (probably at most four or eight bytes). I wouldn't worry about this and would just write the most readable and maintainable code that you can. Commented Jun 19, 2012 at 19:09
  • @templatetypedef Great response, thanks a lot. Commented Jun 19, 2012 at 19:29
  • There is great lecture by standford professor Mehran in youtube (type standford java course) on how these references and objects memory allocation happen. It should be either class 10,11 (or) 12. When you some time watch that video, it really clarifies your dilemma here. Commented Jun 19, 2012 at 19:44

3 Answers 3

4

The first one will allocate a named variable in the current stack frame. On the heap, there is no difference - or there shouldn't be but the VM is of course free to optimize the code in some way as long as the rules are obeyed.

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

5 Comments

So the FileInputStream object will exist in the heap in both cases, but in the former without any reference to the object in the stack, and in the latter with a reference to the object in the stack? So as @templatetypdef mentions, is the extra 4-bytes (x86) or 8-bytes (x64) coming from the pointer?
Yes. stack and heap are two independent structures. The stack will be one pointer-size smaller with the second version since the pointer to the stream doesn't need to be kept in the stack frame.
That said, this is pure theory. Modern VMs translate the code into something much more optimal and keep local variables in CPU registers, so the code above might not use any stack at all. Even when it's using the stack, the data will stay in the CPU cache until a flush becomes necessary. So you see, this is a pretty hypothetical question and you shouldn't really worry about it.
I see, it was in fact some CS theory I was looking for. Thanks for your response.
There is a difference in the byte code but there may or may not be any difference at the CPU level. The local variable can be optimised away, or alternatively the JVM can store the FileInputStream as it would a local variable.
2

No Difference.Both Are the same.

6 Comments

There might be a slight difference due to the local variable inputFile, though.
Either way though, the same amount memory needs to be allocated
No diff. Local variable just stores a reference to an allocated memory.
@AkhilDev But doesn't a reference consume memory in itself (regardless of how small it is)?
@jesterII - And that reference occupies memory for the period of the execution of the method. So yes, that is 1 extra WORD for a momentary period.
|
0

In the first example, the JVM keeps a reference of the FileInputStream, while the second way the JVM creates an unreferenced object that is ready to be garbage-collected after the execution of the statement.

3 Comments

Not really. The instance of Scanner keeps a reference to the FileInputStream (because it reads the FileInputStream for input during its own lifetime).
That's what you think is logical, but how the Scanner class thinks is a different story :)
The Scanner keeps a reference to a Readable (which is actually an InputStreamReader), which keeps a reference to a StreamDecoder, which keeps a reference to the FileInputStream. I am saying this based on the sources of the classes involved, starting with Scanner.java. The point being: The FileInputStream is not garbage collected.

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.