wood burning stoves 2.0*
The moose likes General Computing and the fly likes Heap and Stack in memory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » General Computing
Bookmark "Heap and Stack in memory" Watch "Heap and Stack in memory" New topic
Author

Heap and Stack in memory

nirjari patel
Ranch Hand

Joined: Apr 23, 2009
Posts: 374
Do Heap and stack pre exist in memory ? By that I mean , is it organized in hardware by the memory chip manufacturer ?
Is there exclusive allocation of physical memory space called heap and stack available in memory or is it created by the OS when a java program runs ?
Is heap and stack created by JVM or by the OS ?
Do all the languages (.net, java, Perl, C++ etc) use heap and stack for storing of variables and objects ?

http://www.maxi-pedia.com/what+is+heap+and+stack, on this link, its stated that memory is organized in three segments. (1)code (2)heap (3)stack.
This leads me to think that, its pre existing in the memory. Its provided by the manufacturer that way.

I am trying to find answers by reading articles, but can not get clarity on this.

Thanks.
Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

Main memory does not have this kind of segments physically... These segments are all virtual and created by the OS for the process...

OS plays a vital role in memory management. Process makes use of system calls of OS to have variables allocated either in stack and heap...

C, C++ and Java utilizes this memory segments...

SCJP 6 [SCJP - Old is Gold]
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
nirjari patel wrote:Do Heap and stack pre exist in memory ? By that I mean , is it organized in hardware by the memory chip manufacturer ?
Is there exclusive allocation of physical memory space called heap and stack available in memory or is it created by the OS when a java program runs ?
Is heap and stack created by JVM or by the OS ?
Do all the languages (.net, java, Perl, C++ etc) use heap and stack for storing of variables and objects ?

http://www.maxi-pedia.com/what+is+heap+and+stack, on this link, its stated that memory is organized in three segments. (1)code (2)heap (3)stack.
This leads me to think that, its pre existing in the memory. Its provided by the manufacturer that way.

I am trying to find answers by reading articles, but can not get clarity on this.

Thanks.


It is not clear from your post that you understand the definitions of heap and stack, so let me babble about that a minute.

Stack: when one method calls another, there are things that need to be saved as context from the calling routine, and space that is needed for local variables in the called routine. All that occupies an amount of space that varies with the method being called, and it stays active in memory (i.e., its values are significant) until the called routine returns. If the called routine calls another routine, another section of stack (sometimes called a stack frame) is allocated for that call, etc. All the implementations of this I've seen involve a contiguous area of memory, where one stack frame is placed immediately above or below the previous one.

Heap: when a program allocates memory for its own purposes, i.e., when it is going to use memory for some purpose other than a declared variable (and sometimes even then!), the memory for that comes from the heap. Most of the space in a Java heap is occupied with the memory containing the values within objects, along with pointers to them, etc.

A main difference between these two: Stack is allocated when a subprogram call is made and deallocated when that subprogram call returns. Heap is allocated when the program asks for it and deallocated according to the language being run; in the case of Java, heap space is recovered during "garbage collection", which is beyond the scope of this post.

So, on to your questions: this could be implemented in many ways. Another responder stated that the allocation, etc., was done by the operating system, but the Java implementations of this I've seen don't do it that way -- the JVM, when invoked, gets a big (BIG) block of memory when it starts up and manages stack and heap within that by itself, no OS calls unless it decides to increase the overall amount of memory it has.

You refer to the "manufacturer", which normally refers to hardware; the hardware people have very little to do with the implementation of stack and heap. They make machines that execute a set of instructions and change values in registers and memory according to those instructions, and interact with the outside world through I/O ports and such -- the languages you mention do use both heap and stack, but could implement them quite differently for each language, or even two implementations of the same language. So, usually, there is nothing in the hardware that dictates how heap and stack are implemented.

rc
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
Moving thread as too difficult for "beginning Java™2
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Heap and Stack in memory