jvm memory

  • why use heap?
    • because of priority of things to be done
  • heap operation
    • insert
      • added to bottom, then compare with parent …
    • delete
      • always delete the root element
  • heap vs. java stack
    • Whenever an object is created, it’s always stored in the Heap space and stack memory contains the reference to it.
    • Stack memory only contains local primitive variables and reference variables to objects in heap space
    • Scope
      • Stack values only exist within the scope of the function they are created in. Once it returns, they are discarded.
      • Heap values however exist on the heap. They are created at some point in time, and destructed at another (either by GC or manually, depending on the language/runtime)
    • Stack memory is used to store local variables and function call.
    • While heap memory is used to store objects in Java.
    • Sizeof stack memory is a lot lesser than the size of heap memory in Java.
    • OwnerVariables stored in stacks are only visible to the owner Thread while objects created in the heap are visible to all thread. In other words, stack memory is kind of private memory of Java Threads while heap memory is shared among all threads
    • What makes one faster?
      The stack is faster because the access pattern makes it trivial to allocate and deallocate memory from it (a pointer/integer is simply incremented or decremented), while the heap has much more complex bookkeeping involved in an allocation or deallocation. Also, each byte in the stack tends to be reused very frequently which means it tends to be mapped to the processor’s cache, making it very fast. Another performance hit for the heap is that the heap, being mostly a global resource, typically has to be multi-threading safe, i.e. each allocation and deallocation needs to be – typically – synchronized with “all” other heap accesses in the program.


This entry was posted in Knowledge. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s