Skip to main content

Command Palette

Search for a command to run...

What is heap memory in depth ?

Updated
4 min read
What is heap memory in depth ?
S

I love learning about technology and sharing that with others

what is heap space

  1. heap memory --> dynamic memory which is alternative to local stack memory.
  2. A local stack memory is created whenever we are lets say calling a function at that time the local variables of this function are assigned some memory.
  3. programmer created this memory and its size is predenifed
  4. heap memory can grow as much reuqired in size, so there is a problem to this since in c and c++ we can control this memory space lets we created a bunch of objects and then allocated some memory what if we did not deallocated this memory what will happen the memory cannot be used by other objects and hence eventually the program will die since there is no memory to use this is called as memory leak in java we don't have to worrry about this once the object is derefrenced the garbage collector will automatically collect these memory block,s
Advantages
  1. Lifetime : in local memory menas stack once the function is finihsed execution the local variables will also be gone but that is not the case with dynamic memory same object can stay for longer time.
  2. Size : ex we can declare string buffer and it can store any size of the string we do not have to worry about the size here, but lets say we are using string buffer in a function we have to make sure the size is appropiate to store the string ?
disadvantages
  1. more work: since heap is allocated separately there is more work in this
  2. more bugs : since it is creatted by the programmer there is huge chance that it can lead to some error

  3. Garbage collection will be triggered based on the size of the heap memory, and it will automatically triggered we cannot force the garbase collector to free the memory, hence less control in the developer hands. when the GC is called it will called the finalize() method of the class so the the programmer can do some cleanup in this function call.

Examples 1 :
  • here after the if block we cannot access the employee object and hence it is out of scope and ready for the garbage collection.
void test(boolean found)
{
  if (found)
  {
    // employee1 is a variable local to this if statement
    // but the Employee object is in heap memory.
    Employee employee1 = new Employee("John", 1000);
    // We can do things here with employee1, and its associated object
    Printout(employee1.name());
  }

  // At this point, the only reference to the created object is out of scope
  // since employee1 was local to the if statement block.
  // So the Employee object will be eligible for garbage collection.
}
Example 2
  • here once the employee object is out of scope or derefrenced the Date object is also referenced and become eligible for the GC
// Date class to represent dates
class Date {
  int day;
  int month;
  int year;

  // Date constructor
  public Date(int d, int m, int y)
  {
    day = d; month = m; year = y;
  }
}

// An Employee class that includes a Date
class Employee {
  String name;
  Date dateOfBirth;

  // Employee constructor to initialize both name and date
  public Employee(String name , int d, int m, int y)
  {
    this.name = name;
    dateOfBirth = new Date(d, m, y);
  }
}

// Now let's test this code
class Test {

  public static void main() {
    Employee empPtr = new Employee("Sam", 3, 9, 1983);
    // Now empPtr references an object that contains a reference to a Date object

    empPtr = null; // Now the Employee object is eligible for garbage collection.
    // So its Date object will also be eligible for garbage collection
    // because nothing else points to it.
  }
}
  • Heap Manager : when ever we allocate a new memory heap manager is being called and that will take care of assining a contigous block of memory to us.
  • when all the heap memory is being used an exception is being thrown OutOfMemoryException
  • the allocation and size of the heap is fixed once created , this block of memory is not being assigned to anyone else unless and until it is deferenced and garbase collected all this information is stored in private data stucture of the heap manager.
  • JVM calls the GC to collect the memory and assign it back to the heap manager as a free memory.

Heap memory provides greater control for the programmer—the blocks of memory can be requested in any size, and they remain allocated until they are no longer pointed to and recovered by the garbage collector. An object in heap memory can be passed back to the caller of a function, since it is not deallocated when that function exits. And it can be used to build linked structures such as linked lists and binary trees. The disadvantage of heap memory is that the program must make explicit allocation calls to manage the heap memory, and the program has to wait when the garbage collector runs. The heap memory does not operate automatically and conveniently the way local memory does.

checkout the original link : https://opendsa-server.cs.vt.edu/OpenDSA/Books/CS2/html/HeapMem.html

More from this blog

H

hashcodehub

271 posts

Consistent, Passionate and Organized :)