EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

What is Heap Memory?

By Priya PedamkarPriya Pedamkar

Home » Software Development » Software Development Tutorials » Computer Tutorial » What is Heap Memory?

What-is-Heap-Memory

Definition of Heap Memory

Heap memory is a part of memory allocated to JVM, which is shared by all executing threads in the application. It is the part of JVM in which all class instances and are allocated. It is created on the Start-up process of JVM. It does not need to be contiguous, and its size can be static or dynamic. Space allocated to the memory is reclaimed through an automatic memory management process called garbage collection. Heap memory is a shared area that is utilized during the runtime of Java applications. It is created during the instantiation of Java Virtual Machine (JVM).

This memory is shared by instances of all the classes created during the runtime of an application. As per system configuration, the size of heap memory may be fixed or variable. In order to reclaim the space of heap memory, an automatic memory management process called garbage collection is triggered by JVM. JVM provides control to developers to vary the size of heap memory according to requirement.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

The functioning of Heap Memory

Java Heap memory is basically divided into two parts:

1. Young Generation

This is a part of java heap memory in which newly created objects are allocated and fully reserved for allocating objects. When space allocated to the young generation gets filled, a process called Minor GC (Garbage collection) is triggered by JVM to clean up space by removing unreferenced objects. The young generation is further divided into the following parts:

  • Eden Space: All newly created objects are first allocated to the Eden space of a young generation. It is a larger area of the young generation, and after this Eden space gets filled, Minor GC is triggered, which removes unreferenced objects and moves referenced objects that are survivor objects into another part of the young generation called survivor space.
  • Survivor Space: Collection of objects which survived after Minor GC is moved to this area. Survivor space is further subdivided into two parts called survivor to (s0) and survivor from (s1). At the time of Minor GC, objects from one survivor space are moved to other survivor space that is one of the survivor space is always empty. After many cycles of minor GC, survived objects from the young generation are moved to the old generation. Generally, objects are moved to the old generation after crossing the threshold set corresponding to the young generation’s age.

2. Old Generation

Long living objects are moved to the old generation. Objects survived after many cycles of minor GC are considered old enough to be accommodated into this old space. When space in the old generation gets filled, Major GC (Garbage collection) is triggered by JVM to clean up old space resources. Usually, the process of major garbage collection takes more time than minor garbage collection. 

If garbage collection is not able to clean space to accommodate new objects, an OutOfMemory error is thrown by JVM. To overcome this error, heap size needs to be increased, or proper memory management is required, which can be done through proper understanding of objects created during the application and which objects are taking more space.

Importance

The importance of heap memory can be summarized using the following points:

Popular Course in this category
Java Training (40 Courses, 29 Projects, 4 Quizzes)40 Online Courses | 29 Hands-on Projects | 285+ Hours | Verifiable Certificate of Completion | Lifetime Access | 4 Quizzes with Solutions
4.8 (8,934 ratings)
Course Price

View Course

Related Courses
Windows 10 Training (4 Courses, 4+ Projects)JWS Java Web Services Training (4 Courses, 11 Projects)
  • With the help of heap memory, we can find the smallest and largest number.
  • Garbage collection runs of heap memory to free up space.
  • Heaps memory allows global access to variables.
  • There is no limit on memory size.
  • The priority queue is implemented using heap memory.

Examples of Heap Memory

Following are the different examples of Heap Memory.

Example #1

Now we will see a java example showing how memory is allocated.

Code:

package com.edubca.javademo;
class StudentData {
int rollNumber;
String name;
public StudentData(int rollNumber, String name) {
super();
this.rollNumber = rollNumber;
this.name = name;
}
public int getRollNumber() {
return rollNumber;
}
public void setRollNumber(int rollNumber) {
this.rollNumber = rollNumber;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Main {
public static void main(String[] args) {
int id = 11;
String name = "Yash";
StudentData s = null;
s = new StudentData(id, name);
System.out.println("Student Id is " + s.getRollNumber());
System.out.println("Student Name is " + s.getName());
}
}

Output:

What is Heap Memory 1-1

Memory Allocation:

Now we will see how memory is allocated in the above program:

  • In the Main class, after entering the main method, since id, the name is local variables a space in stack memory is created in the following way:
    • Integer id having primitive value will be stored in stack memory.
    • Reference of StudentData object s is stored in stack memory pointing to the original StudentData object, which is stored in heap memory.
  • Call to StudentData class constructor will further get added to the top of stack memory. The following will be stored:
    • Reference to calling object.
    • Integer variable id having value 11.
    • Reference of String type variable name which will point to an actual object stored in a string pool in heap memory.
  • Two instance variables with the name studentId and studentName declared in StudentData class will be stored in heap memory.

Example #2

Now we will show when JVM throws OutOfMemory error.

Code:

package com.edubca.memoryleak;
public class Main
{
public static void main(String[] args)
{
int[] arr = new int[888888888];     //allocating huge memory
System.out.println("OutOfMemoryError Occurred");
}
}

Output:

What is Heap Memory 1-2

The reason for OutOfMemory error is that we are trying to allocate more memory to an integer array than the available space.

Recommended Articles

This is a guide to What is Heap Memory? Here we discuss the definition of Heap Memory, it’s functioning, along with different examples and code implementation. You may also look at the following articles to learn more –

  1. Java Heap vs Stack
  2. Stack vs Heap Memory
  3. Memory Allocation in C
  4. Types of Memory in Computer

Java Training (40 Courses, 29 Projects, 4 Quizzes)

40 Online Courses

29 Hands-on Projects

285+ Hours

Verifiable Certificate of Completion

Lifetime Access

4 Quizzes with Solutions

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
Computer Tutorial
  • Computer Basics
    • New Technologies of Computer
    • Application of Computer Graphics
    • Types of Computer Architecture
    • Types of Computer Software
    • Line Drawing Algorithm
    • SOAP Web Services Interview Questions
    • Web Services Interview Questions
    • Microservices Interview Questions
    • What is Windows 10?
    • Windows 10 Desktop Mode
    • Windows Commands  
    • Windows Interview Questions
    • Windows Networking Commands
    • Windows Operators
    • Windows Server Interview Questions
    • Introduction to Windows
    • Top Windows Phone Apps
    • Windows Monitoring Tool
    • Productivity tricks for Windows 10
    • Computer Network Advantages and Disadvantages
    • Computer Network Interview Questions
    • Introduction To Computer Network
    • What is Embedded Systems
    • Components of Embedded System
    • Embedded Systems Security
    • Types of Computer Network
    • Types of Computer Operating System
    • Evolution of Operating System
    • NAS Storage Device
    • Windows Administrator Interview
    • Types of Communication Network
    • What is Automata
    • Types of Mainframe Computers
    • Types of Computer Hardware
    • Types of Sensors
    • What is Server
    • What is RPC
    • What is Microservices
    • Types of Computer Virus
    • Types of Computer Keyboard
    • Types of Motherboard
    • Sensor Device
    • What is Computer Graphic Design
    • Computer Architecture Interview
    • What is Computer Science?
    • What is Bluetooth?
    • What is VLAN?
    • Types of VLAN
    • Types of LAN
    • How does LAN switches work
    • What is VLAN Network?
    • What is Native VLAN?
    • How does LAN Switches work?
    • Switching Modes
    • What is Storage?
    • What is Object Storage?
    • What is Block Storage?
    • Block Level Storage
    • Cordova Local Storage
    • Threads in Operating System
    • What is Gateway?
    • What is Ethernet?
    • What is Virtual Machine?
    • What is a Trunk Port?
    • VPN Applications for Android
    • What is a NAS Drive?
    • What is Ring Topology?
    • Point to Point Topology
    • What is Mesh Topology?
    • Extended Star Topology
    • Token Ring Topology
    • Physical Topology
    • What is a Hybrid Topology?
    • Hybrid Network Topology
    • Star Network Topology
    • Star Bus Topology
    • Zariski Topology
    • Ethernet Topology
    • Logical Topology
    • Partial Mesh Topology
    • Types of Computer Cables
    • Types of Computer Language
    • Features of Operating System
    • Functions of Operating System
    • Multithreading in Operating System
    • Ethernet Frame Format
    • Types of Memory in Computer
    • What is Heap Memory?
    • What is Register?
    • Types of Registers
    • What is Arduino?
    • Arduino Operators
    • What is iSCSI?
    • Computer Science Interview Questions
    • Types of USB Ports
    • What is Port
    • Daisy Chain Network
    • What is a Monitor?
    • What is Printer?
    • Types of Printers
    • What is WPS
    • What is Mouse?
    • Types of Socket
    • Transmission Modes
    • Computers Output Devices
    • Memory Units
    • Secondary Memory
    • What is Memory Card?
    • Types of Memory Cards
    • What is Intranet?
    • Central Processing Unit
    • Computer Ports
    • What is CPU Register
    • Types of CPU
    • Types of Compact Disc
    • Cache Memory Types
    • What is a Motherboard?
    • Types of LED
    • Types of Processor
    • Types of Primary Memory
    • Helpdesk Tool
    • What is Optical Fiber?

Related Courses

Windows 10 Training Course

Java Web Services Training Course

Java Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - Java Training (40 Courses, 29 Projects, 4 Quizzes) Learn More