Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

In Java/JVM, why do we call the memory place where Java creates objects as "Heap"?

Does it use the Heap Data Structure to create/remove/maintain the objects?

As I read in the documentation of Heap data structure, the algorithm compares the objects with existing nodes and places them in such a way that Parent object is "greater" than the children. ( Or "lesser" in case of min heap). So in JVM, how are the objects compared against each other before placing them in the heap?

share|improve this question
4  
Unfortunately, I cannot suggest closing it as a duplicate, because an exact duplicate is on a different site. Here is a link to it. –  dasblinkenlight Sep 18 '12 at 15:05
add comment

5 Answers

up vote 7 down vote accepted

The heap as in the memory available for dynamic memory allocation has nothing to do with the heap as in any data structure or the heap invariant (which is in fact related to the data structure). "Heap" is just a common word, and was chosen for both concepts independently (Wikipedia notes that the term was used for data structure first, but no mention of influence).

While it is probably possible to construct a memory management system which uses a heap data structure (or rather, a priority queue) in some places, I am not aware of any existing algorithm that does it, and I doubt it would be useful, outside perhaps some very specific niche.

share|improve this answer
add comment

If you wanted to use a Heap to implement garbage collection you'd probably order the objects by reference count in a Min-Heap, regularly pull the zeroed ones off. I'm not saying this is how it's done or anything, but a cheap n quick possibility.

share|improve this answer
3  
It's a horrid possibility, especially since many heap data structures (especially the simple, fast ones) are awful at increasing and decreasing the key. It also means relying on reference counting, which is not adequate garbage collection. Let's not even get into how an explicit heap data structure is unnecessary, or into implementation details of such a structure. As interesting as what if-questions are, this simply does not make any sense at all. –  delnan Sep 18 '12 at 16:10
1  
Most memory heaps require random access. The Java and C# heaps are more like hashsets than heap structures; there's an array indexed by "reference", which contains the actual pointers out to memory (so the GC can rearrange objects in memory as it cleans up, without affecting usage of those objects by the program). –  KeithS Sep 19 '12 at 14:28
1  
@KeithS Actually a lot of moving GCs, including the one in Microsoft's desktop CLR, do use direct pointers. That does require them to update pointers when moving objects, but they manage to keep the overhead acceptable. The big advantage is that you don't need a huge cache-hostile indirection for every single memory access. –  delnan Sep 19 '12 at 15:42
add comment

The term "heap" here does not relate to the data structure; instead it has the more general meaning of "a big pile of memory to throw things on top of", as opposed to the "stack", which is much more strictly ordered. C#'s "heap" is similar.

share|improve this answer
add comment

The heap in JVM is not related to the data structure, it is more like a space, where objects created by "new" operator are allotted memory at run time. Memory management on heap is done more by de referencing of objects that are no longer in use. As of now,not sure of what algorithm is used, but we have the garbage collector program that determines which objects are no longer referenced by a program, and they are removed from the heap.

share|improve this answer
add comment

The area used for dynamic allocation is traditionally called a heap because this is how the first dynamic allocators ordered the memory- in a heap, by block size. Of course, the JVM's memory management has got nothing whatsoever to do with a heap, and more modern memory allocators also have little in common with it. But that is where the term originates.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.