• Post Reply Bookmark Topic Watch Topic
  • New Topic

Will there be memory allocation difference in these two declrations?  RSS feed

 
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there

I have two code snipshot



and another one is



notice that in second case I am intialzing and declartion variable a

my question is in these two syntax will my program will take different memory model?

Thanks
 
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dint understand your question. In first case 'a' is local to the method in which it is defined (same stack trace )
and second one is local to the for loop stack trace.
 
Bartender
Posts: 1838
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And my answer to this would be: Do you really care? Along with a recommendation to google Premature Optimization.

The answer would probably depend on the compiler/jvm you use to compile and run this code.
As already pointed out by Tushar, the second example has the variable declared inside the loop.

Looking at the code literally, as that variable only has scope during one iteration of the array, theoretically, the memory for that variable would get reallocated each time through the loop.
That would be a difference to the first example, where it is allocated once, and reused for all iterations of the loop.
That might indicate that the first is "better" than the second, because it allocates less variables.


However it doesn't take into account the compiler/JVM.
I am fairly certain that if it sees code like the second example, the JIT compiler will actually modify it to be like the first.

Googling found me someone who actually did this experiment
He found no measurable differenct.

Bottom line: I would go with Option 2, because it communicates the intention of the code clearer. The variable is only intended to be used in the loop, therefore it should be declared within that loop to limit its scope.
Even if (which I doubt) there is any measurable performance difference, it would be so insignificant that I would still prefer the clarity over the nanosecond that might be saved.



 
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:Looking at the code literally, as that variable only has scope during one iteration of the array, theoretically, the memory for that variable would get reallocated each time through the loop.


I fully agree with your post, but this part is incorrect. Variables are not allocated more than once per method. It doesn't matter where in the method you declare them, whether it's as a parameter, in a for loop, or at the very bottom; space for variables is reserved on the stack frame as soon as the method starts running.

The only difference in the piece of code is the unnecessary initial assignment of 0 to a. Which is an even more insignificant difference than the one you talked about earlier.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!