• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Rob Spoor
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:

Memory allocation in Java

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I am new to Java Tech., can anybody explain me how stack and heap works in Java for memory allocation.

Thanx in advance
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Pravin,

The heap stores all java objects whereas the stack holds local variables.

Best regards,
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Simply put...
  • Primitive types (short, char, byte, int, long, float, double) are allocated in the Stack.
  • Object are allocated in the heap, while their references are allocated in the Stack.
  • Objects in the heap are subject to Garbage Collection while primitives in the Stack are not.


  • For more general info how Stack and Heap memory work see this.
    [ January 27, 2005: Message edited by: Vicken Karaoghlanian ]
     
    Ranch Hand
    Posts: 3061
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Vicken Karaoghlanian:
    Simply put...

  • [list] Primitive types (short, char, byte, int, long, float, double) are allocated in the Stack.
  • Object are allocated in the heap, while their references are allocated in the Stack.
  • [ January 27, 2005: Message edited by: Vicken Karaoghlanian ]


  • This is not entirely true. If the primitive types or object references are LOCAL variables, then they ARE allocated on the stack. However, if they are instance or class variables instead, they are allocated in the heap. (Of course, this was mentioned above, but I just wanted to reiterate.)

    Layne
     
    It's just like a fortune cookie, but instead of a cookie, it's pie. And we'll call it ... tiny ad:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic