Results 1 to 10 of 10
  1. #1
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default The Stack and the Heap

    So as I learn a bit more about what seems to be the never ending curve of programming lol I keep coming across words like Stack and the Heap. So far as I think I know it variables like int are on the Stack and classes are on the heap, maybe I am wrong but that is not really my question, my question is,

    What on earth is the Stack and the Heap :D how code is treated in memory is the best I can gather but I want to know more.

  2. #2
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    Stack is a list, like ArrayList. ArrayList has all these methods like add(int index), append(int index), remove(int index), get(int index), etc. Stack only has 3 default methods: Object peek(), void push(Object o), and Object pop(). You can have more like int search(Object o).

    Think of Stack like a pile of cafeteria trays. You can only see the top tray, take out the top tray, and put something on the top tray. With that said:

    Object peek(): returns the top element of a Stack and leaves the Stack list unchanged.
    void push(Object o): puts the Object o on top of the Stack.
    Object pop(): returns the top element of a Stack. It also removes it so Stack list is altered.

    A heap is a complete binary tree.

    I honestly think Google will be more insightful.
    Last edited by Lil_Aziz1; 06-25-2010 at 01:31 AM.
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  3. #3
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Thanks for the reply, yeah I would probably be better served googling first before I ask.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default

    I think the stack and heap that the OP was talking about are related to how the OS and JVM allocate memory for the use of an executing program. These are very old terms.
    Temporary variables like those local to a method are allocated space in the stack area, more permanent things like objects are allocated space in the heap. Also when a program calls another program, it writes into the stack whatever it needs to be able to restore the environment back to what it was when the called method returns. Hence when you have a recursive method go too far, you get a Stack overflow.

  5. #5
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    yup Norm, that was what I was looking for thanks. Should I worry myself about it, or is it really just for way advanced programmers?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default

    Some times you need to give the java command a larger heap. There is a -X parameter for that. Read the API doc for the java command for the different ways to use it.

  7. #7
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Ok thanks for the advice. I imagine you are talking about unreliable classes in terms of usage.....

  8. #8
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    Is it possible to know how much heap space your program is using? I wanna know what makes an application use more than 128mb of heap size. (curiosity)
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default

    how much heap space your program is using?
    Sort of. Look at the Runtime class. It has methods give return memory usages.
    Write a small test program that allocates space (a large array) in a loop and print out the values as you go around to get a feel for what the numbers represent.
    They're very approximate and not predictable because of garbage collection and other hidden things

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default

    You can also take heap dumps, which are memory snapshots usually used for trying to debug OOM exceptions, or excessive memory use.

    jhat and jmap are the supplied tools for that, but most IDEs have better analysis tools generally...once you have the dump available.

Similar Threads

  1. no heap space... need more heap
    By anupam.kumar in forum Advanced Java
    Replies: 3
    Last Post: 02-08-2010, 04:42 PM
  2. Help with this stack application
    By le_albina@hotmail.com in forum New To Java
    Replies: 5
    Last Post: 05-20-2009, 08:07 PM
  3. Stack Trace
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-10-2007, 05:29 PM
  4. Help with heap and stack
    By coco in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 02:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •