This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes heap and stack Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "heap and stack" Watch "heap and stack" New topic
Author

heap and stack

kapil munjal
Ranch Hand

Joined: May 11, 2004
Posts: 298
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


Kapil Munjal
SCJP 1.4, SCWCD 1.4
Eddie Vanda
Ranch Hand

Joined: Mar 18, 2003
Posts: 281
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.


The nice thing about Standards is that there are so many to choose from!
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Objects are placed on the heap.
Stack is used mainly for passing parameters to functions and therefore contains only primitives and object references.


42
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
kapil munjal
Ranch Hand

Joined: May 11, 2004
Posts: 298
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: heap and stack
 
Similar Threads
Object and Varaible
Declaration difference
Static variables must store ?
implementation for static and non-static methods stored in memory
Java Heap and Stack