Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

heap and stack

 
kapil munjal
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

I need to ask about garbage collection. I have come to know that variables are stored in stacks whereas objects are stored on heap...Could anyone explain this concept or please tell me a good tutorial where I could find complete information about the storage of variables and objects.

Thanks
Kapil
 
Eddie Vanda
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try http://www.javaworld.com/javaworld/jw-08-1996/jw-08-gc.html. Other ranchers might have better references.

Generally, each thread has a stack for methods to put their local variables and return addresses on. Objects are only pointed to by object variables.

Objects are placed on the heap. There is the class itself with its static methods and variables, and then there are the instantiations (possibly multiple) of most of these classes.
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Objects are placed on the heap.
Stack is used mainly for passing parameters to functions and therefore contains only primitives and object references.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya know, I've never given heap or stack a second thought. I was offended that C made people thing about such things and pleased that Java doesn't. Am I missing something? Should I care?
 
kapil munjal
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think yes you are missing something in java. If you go through the link given in the first reply then you will know about. I feel that till you know how the memory is managed in your program, you can not make good programs. As long a program becomes, its memory become more crucial. Most of the programmers doesn't know what is going behind and Java prevents programs from worrying about the memory management. But if you know how JVM handles memory then you are one step ahead.

Kapil
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by kapil munjal:
I feel that till you know how the memory is managed in your program, you can not make good programs.


Well, I'm going to play devil's advocate here. Please describe a case where detailed knowledge of Java memory management has help you to write better software. I think we can exclude the obvious and trivial case of not retaining references to objects that ought to be garbage collected; I want to hear something more juicy.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm fairly deeply familiar with computer memory allocation, having done pretty tricky bits in mainframe assembler and older PC languages with segmented memory, overlays and manual memory management. One of the great values of Java is taking these issues off my plate and letting me worry about business. It has a great ratio of lines doing my business to lines keeping the computer happy.

I thought I maybe had an example of where I'd care about heap vs stack, but it didn't pan out. Color me blisfully uncaring.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic