Jump to content

What are HEAP and STACK memory?


albertlee

Recommended Posts

When I was learning Java, the tutor use to use HEAP and STACK memory as example to teach how does Java work on the method and data of the objects.....

 

 

but is there any clear, well-explained explanation/concept/definition on STACK and HEAP memory?

 

 

ALbert

Link to comment
Share on other sites

Stack men is the mem map in a system, a PC uses 2, the ram you can add to and the ram that`s the base mem 640k I think it is on a pc?

but within that stack will be the rom(s) and h/ware chip registers as well as IRQ adresses.

as for Heap mem, I don`t know, it sounds like what we used to call "Chunk" mem, don`t quote me but I think it`s used for systems with a dynamic stack pointer and exploits mem offsets. I could be wrong though!!!!

Link to comment
Share on other sites

Well, YT, what my Java tutor said briefly about STACK and HEAP is that STACK is where the procedure of java object takes place, and it points to HEAP, where is somehow the store of data/method......

 

But what I really need is how both STACK and HEAP are different in the aspect of programming language (like Java)? since what my tutor tell is very brief, I cannot have a thorough understanding on it at all........

 

Any help?

 

Albert

Link to comment
Share on other sites

This is all dependant on the language although the fundementals are the same. Stacks are basically stores of whatever your running, in java and most programming lang's you can either push or pop into / out of stacks - that's the very basics.... if you don't know what I'm talking about, then I've misunderstood you :). Example

 

Example java.lang.Object pop(!)

throws java.example.exception

java.example.exception

 

 

A java heap is a free store memory location, BUT the only way you can use it to store is to ref. what your wanting to store, for example if you wanted to store an array in an applet you need to flag / ref. it to the heap.

Link to comment
Share on other sites

Well, what my java tutor give on examples are that stack memory is where an object which contains method, such method points (u know, unlike C, java can point out for you) to the Heap, where stores the data/method in another object......

 

I think I want to know the definition of Stack and Heap by now,, since I know the concept of both on Java, but it really feels odd to me as I know the concept, but don't know what they actually mean..........

 

P.S. Another personal wondering, will we understand Heap and Stack better if I go straight studying ASM?

 

 

 

 

Any furthur help?

 

 

ALbert

Link to comment
Share on other sites

P.S. Another personal wondering' date=' will we understand Heap and Stack better if I go straight studying ASM?

Any furthur help?

 

 

ALbert[/quote']

Stack mem is ESSENTIAL in ASM :)

as are the other things I`ve mentioned. but you`ll learn about them in good time with proper lessons, they`ll start you of with basic computer architecture and then focus more on the cpu and its componants and functions. from there you`ll learn basic cpu op codes and their mnemonics.

you`ll do all this and learn it long before anyone passes you a mem map and says get on with it :)

Link to comment
Share on other sites

I know the definition for a stack.

 

First of all, you have to allocate yourself some memory, and the allocation must be of a fixed size; i.e. int[20], char[15] etc. You then have a pointer which points to the address at the top of the stack.

 

Now basically the two operations you can do are to push things onto the stack, and pop things off of the stack. When you push things onto the stack, you increase the address of the pointer to point at the new object, and when you pop things off you decrease the address to the previous object. All of this means that you have a FILO data structure, which is useful for the reasons mentioned earlier in the thread.

 

As far as I'm aware, a heap is just an area of memory that can be accessed via a hashing algorithm (i.e. some algorithm that determines the memory address of the data you're putting onto the heap).

 

Hope this helps.

Link to comment
Share on other sites

  • 1 month later...
  • 2 weeks later...

Both stack and heap are areas in memory. The difference between the two comes about from there usage (in terms of memory management).

 

A stack is an area of memory that behaves like a stack ADT. It is used for storing information/objects that will not grow in size and will not be moved around in memory (in other words, the stack is used for storing static data objects). The stack requires very little janitor work from the OS/programmer.

 

The heap on the other hand, is a dynamic area of memory. You can use the heap to create data objects that are likely to grow (like vectors, maps, sets, etc.). The heap tends to suffer from fragmentation issues and needs to be garbage collected every so often to maintain its usability. Hence, the heap require much intervention from the memory management system of the OS or the programmer.

Link to comment
Share on other sites

  • 10 years later...

Stack is used for static memory allocation and Heap for dynamic memory allocation, both stored in the computer's RAM .

 

Variables allocated on the heap have their memory allocated at run time and accessing this memory is a bit slower, but the heap size is only limited by the size of virtual memory . Element of the heap have no dependencies with each other and can always be accessed randomly at any time. You can allocate a block at any time and free it at any time. This makes it much more complex to keep track of which parts of the heap are allocated or free at any given time.

 

In a multi-threaded situation each thread will have its own completely independent stack but they will share the heap. Stack is thread specific and Heap is application specific. The stack is important to consider in exception handling and thread executions.

 

More about......Stack and Heap

 

Warner

Link to comment
Share on other sites

The biggest difference between stack and heap is how they are shared (or not) in a multithreaded application.

 

The stack (see also stacktrace) is the memory space a thread runs in, and is dedicated to that thread.

Heap memory is shared by all the threads in an application (and causes issues for thread safety if not accounted for).

 

More information here: Heap and Stack

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.