Sunday 9 October 2011

every time compiler delete unused stacks why ?

every time when machine starts it create stacks and every time when we switch off our machine ( JVM ) it deletes all its stacks .

yes what are you thinking for the answer of this question is true .. that memory allocated for that for program should be deleted ... i will say yes but take a case :

create a for loop :-
int i ;
for( i = 0 ; i < 100 ; i ++ )
{
  ;
}

now for this program think how much memory is allocated .
in your mind you are thinking that not too much just some addressing because , starting address is stored
or a return address for this program ... yaa thats true in your sense ...

but compiler is too different from our sense because it has to create something which is accepted by machine .
now think like a compiler and dream how much memory will allocate if each instruction takes 2 byte ....
please think ... think ... think ... because when you think your mind makes some move and increases your level .

if i say that this program will take minimum ------ bytes than you will say that how ...
but this is true .... because every time a instruction is loaded from frame and stored into operand stack ... so two instruction is used for only 1 value and every time the old value is fetched from operand stack and every time it is incremented and loaded back into operand stack than 3 instruction is used for still one operation
so minimum 10 bytes which is allocated to 1 instruction and there are total 100 instruction and task are performed and total 1000 bytes means 1kb memory only for a for loop .... wow ( i love it )

now i think this is enough for finding why stack is deleted and for me and for compiler 1 kb is enough but not for you ,,, so please spent your sometime in machine representation because every time you will find something great which will make you different from others .

and learn more and google more because life is not only for fun you are here for doing something not for wasting your rime ...

No comments: