• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Primitives and references

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is garbage collectible heap & what it is used for?

Why do Objects live on heap & not variables & reference variables?

What's its use?

Can Objects exists without heap?
 
Ranch Hand
Posts: 128
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The heap can be seen as the global memory pool. The heap is accessible to all methods in the program as the program runs. Unlike the variables/ primitives which live in the stack, which will go out of scope as soon as the method that declares them exits or returns. The garbage collector reclaims the memory on any objects that are eligible for garbage collection...i.e Objects which can longer be accessed. For example, if you create an object and  later assign that object reference to null. The object still lives in the heap but the link to get to it is dead. Therefore that object can be garbage collected.

 
Marshal
Posts: 80749
486
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't go setting references to null in order to make them eligible for garbage collection.
The heap is a memory space which contains the objects. Local variables exist on the stack, but all fields are inside objects. Instance fields live inside ordinary objects and static fields inside the corresponding Class<?> object. The difference between a primitive and a reference type is that in the case of a primitive, the content of the variable is the value, but in the case of everything else, the content of the variable is a reference allowing the JVM to find the address of the object. If a field points to a reference type, that field (indirectly) is stored on the heap, inside another object.
Yes, it is possible to create objects in other memory locations than on the heap. But you don't actually need to know about that.
 
Ranch Hand
Posts: 393
9
Open BSD BSD Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Why do Objects live on heap & not variables & reference variables?



Memory hierarchies in the computing world(e.g.register, cache, Ram , disk  etc) exist because of efficiency.
Heap and stack are  data structures with different characteristics

The heap is memory used for dynamic allocation; means that you can allocate and/or free a block at any time, In a heap that great flexibility has a price that is: it's   much more complex to keep track of which parts of the heap are allocated or free at any given time. Unlike the stack, there's no a LIFO schema to the allocate/deallocate blocks from the heap. In other words the heap is way slower than the stack(it's  trivial to allocate/deallocate memory theret ,a pointer/integer is simply incremented or decremented)

Another efficiency problem is that heap, is generally a global resource, so is faced to guaranties the safety in a multi-threading environment, i.e. each allocation and deallocation needs to be synchronized with the rest of threads that accesses the heap.

For each thread correspond a stack, while there's typically only one heap for the entire application. Anyway here can be multiple heaps too for different types of allocation for example.

Objects are dynamic things can be very complex and have longer life than e.g. methods so they're posed in heap that can grow at will . Stacks on the other hand tend to be smaller and faster so more appropriate to contain e.g.primitive data or data that have to reused frequently  at a faster rate than in heap

Cheers
Harry G.T. Kar
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic