16

I just read this statement in a java book saying Objects in java reside on a heap. Is a heap used because it is the best way to store data and retrieve data fast ?

I only have an idea about data structures being a beginner. I mean why not stack or something else ?

trincot
  • 317,000
  • 35
  • 244
  • 286
Serenity
  • 4,968
  • 19
  • 65
  • 104
  • 16
    If you're reading a book about data structures, then be aware that there are *two* common meanings of the word "heap" in computer science. One is the tree-like data structure, and the other is a general space for memory allocation. Java's heap is a general space for memory allocation. – Greg Hewgill May 07 '10 at 09:56
  • 2
    I have also been wondering about this. It's funny, because during my first bachelor the term stack in heap were used only in context of programming. At that time I had no idea about data structures. Now that I have studied data structures, I can understand why the stack is a stack, but not why the heap is a heap. I can see in the answers who has studied about data structures and who hasn't :). Actually, the comment above from Greg answers the question best IMHO. As I read it: 'the' heap is not necessarily 'a' heap. http://en.wikipedia.org/wiki/Heap – Matthijs Wessels Sep 06 '10 at 11:46

7 Answers7

16

The problem with a stack is that you can only remove the most recent thing you added. This works fine for local variables, since they come and go as you enter and exit functions, but not so well for arbitrary data who's lifecycle doesn't follow that of individual functions. The memory heap allows you to add and remove data at will.

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
  • This answer is awesome in a way, since it explains why the heap is sometimes better than the stack in a way that makes it sound as if the heap also needs to be a heap data structure, literally. It's also very confusing, in much the same way. – unwind May 07 '10 at 09:59
  • @unwind: The answer is pretty straightforward, nothing confusing really. Which parts do you find confusing? – instantsetsuna Aug 18 '10 at 09:03
3

The heap is used just by the vm to allocate and deallocate blocks of memory. To access the objects there you use a reference to the block of memory (that reference is in the stack). The jvm doesn't allow direct access to the memory (like in C/C++).

Jonas Fagundes
  • 1,519
  • 1
  • 11
  • 18
2

It's an artifact of the underlying computing model. Memory to the operating system looks like a big, mostly contiguous space in which data can be read and written by address. The operating system allows processes to grab a block of memory (a large contiguous space, usually at least one page of a couple K) and to do with that as they like, by using memory addresses and read/write operations.

The java heap builds on that, i.e. to the programmer it just looks like a big bag of memory (it of course is not, i.e. garbage collection routinely moves stuff around) to which he gets "addresses" (references really, they are not actually addresses) for data (objects) written to this memory space. This allows you maximum flexibility to build more specialised data structures on top of that.

Remember that it acts like a "heap" to the programmer, because that allows you the necessary flexibility, but it doesn't have to be implemented as such. It's a piece of memory managed by the garbage collector, and there are a bunch of data structures it uses to do its job, which you could or could not consider part of the heap, i.e. it's memory used and allocated by the JVM, but usually only the memory accessible to the programmer is considered to be "the heap" in this context.

wds
  • 31,873
  • 11
  • 59
  • 84
1

Java can store objects on the stack, if it does escape analysis that determines that no references to the object are retained by non local objects when the method returns. It does not allow you to declare that an object is stored on the stack.

If Java did allow objects to be explicitly on the stack, what would happen when the method returned? What would happen to any references to the local object, held by any non local objects?

  • The Java designers could have decided that some references could be invalid, leading to undefined behaviour if dereferenced. Just like a pointer in C/C++. The Java designers seem to have gone to great lengths to avoid undefined behaviour.

  • The Java designers could have specified that all references to the local object became null. Finding all those references would be difficult. And it would result in difficult to find bugs caused by references that were assumed to be not null suddenly became null. Immutable objects containing object references would be impossible. And the notification mechanism that set references to null would have to work across threads. The cost of all this would be much higher than the advantage of local storage.

  • One of language designers, James Gosling, has a Lisp background, and that influence is visible in the idea that object disposal is just left to the garbage collector, with the compiler or run time environment optimising object disposal (escape analysis) if possible.

Raedwald
  • 46,613
  • 43
  • 151
  • 237
1

Because Java uses a garbage collector to reclaim memory, unlike C and C++. In those languages it makes sense to use the stack for local variables*. In Java, there is no concept of a (local) variable going out of scope, only it is not referenced anymore, which makes it eligible for garbage collection (which is bound to happen sometime after that point).

* which, for the sake of clarity, does not mean that there were no heap in C/C++, or that you could not use the malloc/new family to allocate variables to be used solely within local scope.

Péter Török
  • 114,404
  • 31
  • 268
  • 329
  • C and C++ also use a heap. It's accessed via malloc/free (C and C++) or new/delete (C++ only). The OP wasn't asking about local variables, BTW. – Marcelo Cantos May 07 '10 at 09:54
  • You edited your answer after I posted my comment. Plus, your amended response is still off the mark. The OP wanted to know why one would place objects on the heap ("why not the stack?"). Your answer, "Because Java uses a garbage collector...," is just plain wrong. – Marcelo Cantos May 07 '10 at 10:11
  • @Marcelo Cantos, can you actually place objects on the stack in Java? Moreover, note the title of the post: "Why does Java uses heap for memory allocation?". I tried to answer that, and I still think I gave a reasonable explanation. Of course, feel free to disagree :-) – Péter Török May 07 '10 at 10:20
1

Why not store objects on the stack? Well, what happens to the stack after the currently executing method stops executing?

Anonymoose
  • 5,662
  • 4
  • 33
  • 41
1

Because objects in Java often outlive the scope within which they were created, at which point the stack frame that was created for the scope ceases to exist.

Allocated heap space on the contrary is not deallocated automatically when the scope within which the object was created no longer exist.

Vlad Gudim
  • 23,397
  • 16
  • 69
  • 92