Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 25 of 25

Thread: Stack Memory question (understanding)

  1. #1
    Member
    Join Date
    Sep 2013
    Posts
    102
    Thanks
    38
    Thanked 0 Times in 0 Posts

    Default Stack Memory question (understanding)

    I was recently running into some issues with non-static variables, I think it might be my tenuous understanding of stack memory. So java only gets one stack per thread? And if this is the case it seems really inefficient to hold several objects instance variables in one stack considering I rarely load them in any order that will aid LILO functionality. This makes me want to use an array of threads to handle some objects to aid the memory. So I suppose the real questions are: A, does java only have one stack per thread, B, what takes place in a stack to delete values below the last in.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Stack Memory question (understanding)

    What issue with non-static variables were you having?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Stack Memory question (understanding)

    Are you having problems with java program execution?
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Member
    Join Date
    Sep 2013
    Posts
    102
    Thanks
    38
    Thanked 0 Times in 0 Posts

    Default Re: Stack Memory question (understanding)

    The issue is less of my concern I am mostly just curious about this: "So I suppose the real questions are: A, does java only have one stack per thread, B, what takes place in a stack to delete values below the last in." I feel confident with a better grasp of these questions I can move forward.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Stack Memory question (understanding)

    How will knowing the answers change how you write a program?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Member
    Join Date
    Sep 2013
    Posts
    102
    Thanks
    38
    Thanked 0 Times in 0 Posts

    Default Re: Stack Memory question (understanding)

    I did mention in my initial post some changes I could make, but I would like to just move past this and deal strictly with the questions I asked if you don't mind.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Stack Memory question (understanding)

    I am mostly just curious
    I think worrying about this question is a waste of time for 90+% of the program's that you would write.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Stack Memory question (understanding)

    Quote Originally Posted by KAJLogic View Post
    I did mention in my initial post some changes I could make, but I would like to just move past this and deal strictly with the questions I asked if you don't mind.
    The questions you asked are pretty much answered by a quick google of "java method stack". What exactly are you confused about?

    The reason we're asking you about your actual problem is that it sounds like you're blaming "stack memory" on something simpler, and we want to get at the actual root cause of your problem.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Member
    Join Date
    Sep 2013
    Posts
    102
    Thanks
    38
    Thanked 0 Times in 0 Posts

    Default Re: Stack Memory question (understanding)

    Well for the sake of this conversation let us just trust in my diagnostic ability. The google term you suggested answered one of my questions, but I am still thinking it is seriously inefficient to have one thread handle several objects stack variables. What would java do if I want to delete an index in the stack below the last?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Stack Memory question (understanding)

    want to delete an index in the stack
    What is the java package and class that gives a program access to the contents of the program stack? Sounds like a good way to add bugs to a program.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Stack Memory question (understanding)

    Quote Originally Posted by KAJLogic View Post
    Well for the sake of this conversation let us just trust in my diagnostic ability. The google term you suggested answered one of my questions, but I am still thinking it is seriously inefficient to have one thread handle several objects stack variables. What would java do if I want to delete an index in the stack below the last?
    I think you're really confused about what you mean when you say "stack". A stack is a data structure, and things like method calls are indeed represented by a stack data structure. But you as the programmer don't get direct access to that stack, so you can't remove things from it like your question suggests you can.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  12. #12
    Member
    Join Date
    Sep 2013
    Posts
    102
    Thanks
    38
    Thanked 0 Times in 0 Posts

    Default Re: Stack Memory question (understanding)

    For norm this is the one thing I mentioned.
    This makes me want to use an array of threads to handle some objects to aid the memory
    When I said:
    What would java do if I want to delete an index in the stack below the last?
    It was in an indirect manner. Say I have some values under a region in the stack now what if I happen to delete an object's instances below the last region; my question was how does java handle an event like this, or how does it prevent it form happening. Because I have several objects with instances on one thread (one stack then), how does java deal with a request to delete an early objects instances and still maintain the latest?

    When I say delete below that region I simply mean if I declare some values and they are entered onto the stack, then I get a new object and declare some values if I delete the first object how does java maintain LILO while removing the first object's values? The only alternatives I can see is java won't delete a lower entry, or it copies all higher entries onto a new stack except the desired index (unlikely considering how inefficient it would be). Which makes me want to try some things differently like the aforementioned option.

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Stack Memory question (understanding)

    what if I happen to delete an object's instances below the last region
    request to delete an early objects instances
    How do you do that?
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Member
    Join Date
    Sep 2013
    Posts
    102
    Thanks
    38
    Thanked 0 Times in 0 Posts

    Default Re: Stack Memory question (understanding)

    Instantiate an object set values, then instantiate a new object and set values now try and delete the former object (all on the same thread).

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Stack Memory question (understanding)

    delete the former object
    Can you post some code that shows what you are trying to say?
    If you don't understand my answer, don't ignore it, ask a question.

  16. The Following User Says Thank You to Norm For This Useful Post:

    KAJLogic (January 5th, 2014)

  17. #16
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Stack Memory question (understanding)

    Reading back over this thread, it's all over the place, and I'm not sure what you're asking any more. It ends by posing a simple task that could be accomplished by:

    MyObject object1 = new MyObject();
    MyObject object2 = new MyObject();

    object1 = null; // nominates object1 for garbage collection

    Somehow you've mixed together the concepts of stack and heap memory, which are not under the programmer's control in Java, and the data structure "stack," which the programmer has the degree of control provided by the data structure's interface. You might search and read more on stack/heap memory in Java and then garbage collection. I think all that you need to know about those subjects is included in Effective Java.

  18. The Following User Says Thank You to GregBrannon For This Useful Post:

    KAJLogic (January 5th, 2014)

  19. #17
    Member
    Join Date
    Sep 2013
    Posts
    102
    Thanks
    38
    Thanked 0 Times in 0 Posts

    Default Re: Stack Memory question (understanding)

    Quote Originally Posted by Norm View Post
    Can you post some code that shows what you are trying to say?
    int s = 1;
    int b = 13;
    Assume they are both local, and what if I want to delete s first?

  20. #18
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Stack Memory question (understanding)

    Quote Originally Posted by KAJLogic View Post
    Assume they are both local, and what if I want to delete s first?
    How would you delete s? And what does this have to do with the stack? And how has this lead to problems in your code?

    Again, I think you're confused about the basics, and you're asking questions that you have no way of really understanding at this point. I suggest coming back to this when you understand more about scope, the garbage collector, and program execution.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  21. #19
    Member
    Join Date
    Sep 2013
    Posts
    102
    Thanks
    38
    Thanked 0 Times in 0 Posts

    Default Re: Stack Memory question (understanding)

    Quote Originally Posted by KevinWorkman View Post
    How would you delete s? And what does this have to do with the stack? And how has this lead to problems in your code?

    Again, I think you're confused about the basics, and you're asking questions that you have no way of really understanding at this point. I suggest coming back to this when you understand more about scope, the garbage collector, and program execution.
    Are you saying that local variables (non-static) aren't stored in the stack (I realize references might be stored in heap)? Heap is for objects and static variables is it not, so garbage collector wouldn't really apply? I delete S by making it's class reference null.

  22. #20
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Stack Memory question (understanding)

    Quote Originally Posted by KAJLogic View Post
    Are you saying that local variables (non-static) aren't stored in the stack (I realize references might be stored in heap)? Heap is for objects and static variables is it not, so garbage collector wouldn't really apply? I delete S by making it's class reference null.
    I am saying that it probably doesn't matter, and most of the questions you're asking don't make a lot of sense.

    In your example, s is an int and cannot be null.

    Each thread has a stack. Each method has a stack frame. Each stack frame contains the primitive variables as well as the references to any Object variables. References point to Objects on the heap.

    You can't "delete" a value, and even if you set it to null, nothing dramatic is going to happen. I don't know what you /think/ will happen, but it almost definitely has no consequence in your code.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  23. #21
    Member
    Join Date
    Sep 2013
    Posts
    102
    Thanks
    38
    Thanked 0 Times in 0 Posts

    Default Re: Stack Memory question (understanding)

    Never mind I found it. You guys were right from the beginning I should have just done some more research. It has to do with re-arranging to delete an item not on the top, I can do this in many different ways. And, no you cannot make an int null, but you can make the instance class that it is held in null like I said above. Which should invoke a stack iteration if my understanding is correct.

  24. #22
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Stack Memory question (understanding)

    Quote Originally Posted by KAJLogic View Post
    Never mind I found it. You guys were right from the beginning I should have just done some more research. It has to do with re-arranging to delete an item not on the top, I can do this in many different ways.
    So your question was about the stack data structure and not the thread stack this whole time?

    Quote Originally Posted by KAJLogic View Post
    And, no you cannot make an int null, but you can make the instance class that it is held in null like I said above. Which should invoke a stack iteration if my understanding is correct.
    Your understanding is not correct. Pointing a reference to null does not affect the method stack in any way.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  25. #23
    Member
    Join Date
    Sep 2013
    Posts
    102
    Thanks
    38
    Thanked 0 Times in 0 Posts

    Default Re: Stack Memory question (understanding)

    Quote Originally Posted by KevinWorkman View Post
    So your question was about the stack data structure and not the thread stack this whole time?



    Your understanding is not correct. Pointing a reference to null does not affect the method stack in any way.


    Okay so only local variables are stored in stack. Which are handled when it returns. My question was how does the JVM delete a lower iteration (push/rearranging). And, what do you mean because to my knowledge java only uses stacks with respect to threads.

  26. #24
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Stack Memory question (understanding)

    Quote Originally Posted by KAJLogic View Post
    Okay so only local variables are stored in stack. Which are handled when it returns. My question was how does the JVM delete a lower iteration (push/rearranging). And, what do you mean because to my knowledge java only uses stacks with respect to threads.
    I think you're confused about how references get stored on a stack. They aren't stored one after another, directly on a stack. They are stored in a stack frame, which is then stored on the stack. Modifying the value of one variable does not require Java to dig into the stack or rearrange anything. It simply modifies the reference, which is on the stack frame for the method you're in.

    Say you have a bunch of stuff (shoes, clothes, toys, etc) that you want to arrange in a stack of boxes. You don't just put the shoes directly on top of the clothes, directly on top of the toys. You take a box, put some stuff in it, close it up, then put another box on top of that box. You then fill the second box and continue from there. Now if you accidentally put something in the top box that doesn't belong there, you don't have to go into any of the other boxes- you just reach in and take the item out.

    The stuff is references, the boxes are stack frames, and the stack of boxes is your method stack. But again, the chances of any of these details actually mattering in your code are very low.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  27. The Following User Says Thank You to KevinWorkman For This Useful Post:

    KAJLogic (January 5th, 2014)

  28. #25
    Member
    Join Date
    Sep 2013
    Posts
    102
    Thanks
    38
    Thanked 0 Times in 0 Posts

    Default Re: Stack Memory question (understanding)

    This is all irrelevant, and I assure you my understanding is solid (thanks to you folks); likely your digression is latency from my poor explanation and communication. I was messing around and for the record stack memory should be well understood by programmers, for reaching the upper system limits of even the most impressive computers isn't that hard when dealing with recursion (I will be using thread arrays to handle large events).

Similar Threads

  1. what is difference between call stack and stack tace?
    By me_shankara in forum Exceptions
    Replies: 6
    Last Post: October 27th, 2018, 03:23 AM
  2. Replies: 0
    Last Post: April 3rd, 2013, 09:17 PM
  3. Having problems understanding what is going on in this linked stack.
    By vysero in forum Object Oriented Programming
    Replies: 1
    Last Post: September 27th, 2012, 12:08 AM
  4. Properly releasing memory to avoid memory pileup/crash
    By fickletrick in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 22nd, 2012, 10:09 AM
  5. [SOLVED] Memory usage increasing in while loop - is it a memory leak
    By mds1256 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 18th, 2012, 10:06 AM