4

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?

1

5 Answers 5

9

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.

2

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.

1

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.

3
  • 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.
    – user7043
    Sep 18, 2012 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, 2012 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.
    – user7043
    Sep 19, 2012 at 15:42
1

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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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