• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

garbage collection heap?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am new to programmingjava and I dont understand this quite well, what is a heap and what is a stack? thank you kindly
- snipped this from cups in the campfire stories
***
In Java, objects are created on the heap, and a REFERENCE to the object is stored in the cup. Think of it as a remote control to a specific type of object.
 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Deepak,
Even I have jsut started learnig JAVA...al what I know is Heap u can consider as something like a pipe..first in first out..
Stack is like a jar or a bottle ........last in first out...
Hope this helps...
Anybody please correct me if I am wrong...
Kajol
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi there,
stack and heap both are placed in the RAM(random access memory).
the difference is
stack has something called a stackpointer which is used by the processor to create and free memory.
to remember we can say that primitive datatypes are placed in stack where as object handles are placed in heap.
this is because the stack need to know the exact size and lifetime of all the data it is going to store at compile time..
well in case of obj handles it is decided in runtime.
stack storage is faster to access than heap.
hope is is satisfactory
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess Vijay has correctly summarized the difference between Stack and Heap Memory.
In a computer you have different memory areas. There are registers inside the CPU itself where the computation is carried out. You have a cache between the CPU registers and the RAM which is like a buffer. This speeds up memory access. RAM is subdivided into various areas among which you have Stack and Heap. In addition I believe we have separate memory areas to store the static variables - in the static memory area.
In programming languages like C/ C++ one could directly access the stack memory using pointers. Pointers are not allowed in Java. Accessing stack is much faster than the heap. It just requires incrementing and decrementing the stack pointer. (Stack Pointer is one of the registers in the CPU). But creating an object in the heap requiers some over head. So the operation is slow. The advantage that heap offers is : you can create your objects any time you want. You can declare and instantiate an object when ever you feel the need for it. But if you want to use the stack, then you need to declare the variables beforehand. This is because Stack memory is a limited resource and has to be managed carefully. In addition, the objects/ variables created in the stack have to be explicitly destroyed so as to avoid any stack overflow.
SJ
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I understand this, in Java, the Stack area stores the references to an object. The actual object is stored in the heap area. When an object no longer has something on the stack which refers to it, it is garbage-collected (freed for other use).

In other words, we reference an object(located in the heap) through variables located in the stack. Did I get that right?
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I think all the explanations given are fine. One thing to add. In java we can't create an object on stack, whereas in C++ we can.
Good Luck !!!
 
Uh oh, we're definitely being carded. Here, show him this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic