aspose file tools*
The moose likes Beginning Java and the fly likes Memory allocation in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Memory allocation in Java" Watch "Memory allocation in Java" New topic
Author

Memory allocation in Java

pravin suroshe
Greenhorn

Joined: Jan 13, 2005
Posts: 8
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
Lionel Badiou
Ranch Hand

Joined: Jan 06, 2005
Posts: 140
Hi Pravin,

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

Best regards,


Lionel Badiou
CodeFutures Software
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
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 ]

    - Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. <br />- What truth? <br />- That there is no spoon!!!
    Layne Lund
    Ranch Hand

    Joined: Dec 06, 2001
    Posts: 3061
    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


    Java API Documentation
    The Java Tutorial
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Memory allocation in Java
     
    Similar Threads
    Why Compiler eror here?
    String literal pool
    Do I need to know pointers to program in C++?
    Avoiding memory allocation?
    LINK LIST IN JAVA WITHOUT COLLECTION FRAMEWORK