Stack & Heap

来源:互联网 发布:删除有条件的数据sql 编辑:程序博客网 时间:2024/05/22 02:14
Stack.   This   lives   in   the   general   RAM   (random-access   memory)   area,   but   has   direct   support   from   the   processor   via   its   stack   pointer.   The   stack   pointer   is   moved   down   to   create   new   memory   and   moved   up   to   release   that   memory.   This   is   an   extremely   fast   and   efficient   way   to   allocate   storage,   second   only   to   registers.   The   Java   compiler   must   know,   while   it   is   creating   the   program,   the   exact   size   and   lifetime   of   all   the   data   that   is   stored   on   the   stack,   because   it   must   generate   the   code   to   move   the   stack   pointer   up   and   down.   This   constraint   places   limits   on   the   flexibility   of   your   programs,   so   while   some   Java   storage   exists   on   the   stack   ?in   particular,   object   handles   ?Java   objects   are   not   placed   on   the   stack.  

Heap.   This   is   a   general-purpose   pool   of   memory   (also   in   the   RAM   area)   where   all   Java   objects   live.   The   nice   thing   about   the   heap   is   that,   unlike   the   stack,   the   compiler   doesn 't   need   to   know   how   much   storage   it   needs   to   allocate   from   the   heap   or   how   long   that   storage   must   stay   on   the   heap.   Thus,   there 's   a   great   deal   of   flexibility   in   using   storage   on   the   heap.   Whenever   you   need   to   create   an   object,   you   simply   write   the   code   to   create   it   using   new   and   the   storage   is   allocated   on   the   heap   when   that   code   is   executed.   And   of   course   there 's   a   price   you   pay   for   this   flexibility:   it   takes   more   time   to   allocate   heap   storage.

原创粉丝点击