Close Menu
Java HelpsJava Helps
    What's Hot

    Things To Do After Installing IntelliJ IDEA

    December 20, 2022

    How to Set Proxy for IntelliJ IDEA?

    December 20, 2022

    What is Garbage Collection?

    December 7, 2022
    Facebook X (Twitter) Instagram
    Java HelpsJava Helps
    • Home
    • How To
      1. Install
      2. Run
      3. Configure
      4. View All

      How to Install MySQL with phpMyAdmin on Ubuntu?

      December 4, 2022

      Install The Latest IntelliJ IDEA on Linux

      November 28, 2022

      Install The Latest Apache Maven on Linux

      November 27, 2022

      Install The Latest Oracle JDK on Linux

      November 27, 2022

      Run NiFi Cluster in Docker with SSL Enabled

      December 6, 2022

      How to Run Apache NiFi Docker on Mac M1?

      December 1, 2022

      How to Run Apache NiFi on Docker?

      December 1, 2022

      Create A New Maven Project In IntelliJ IDEA

      November 29, 2022

      Things To Do After Installing IntelliJ IDEA

      December 20, 2022

      How to Set Proxy for IntelliJ IDEA?

      December 20, 2022

      How to Set Proxy for Maven?

      December 6, 2022

      How to Create a Fat JAR Using Maven?

      December 5, 2022

      Things To Do After Installing IntelliJ IDEA

      December 20, 2022

      How to Set Proxy for IntelliJ IDEA?

      December 20, 2022

      Manage GitHub Artifact Storage Quota

      December 6, 2022

      Run NiFi Cluster in Docker with SSL Enabled

      December 6, 2022
    • Hello World
      1. Framework
      2. Library
      3. View All

      JPA Hello World! using Hibernate and MySQL

      December 3, 2022

      Jersey 3.x – Hello World!

      December 2, 2022

      Microservices Framework for Java (MSF4J) – Hello World!

      December 1, 2022

      How to Parse PCAP files in Java?

      November 30, 2022

      JPA Hello World! using Hibernate and MySQL

      December 3, 2022

      Jersey 3.x – Hello World!

      December 2, 2022

      Microservices Framework for Java (MSF4J) – Hello World!

      December 1, 2022

      How to Parse PCAP files in Java?

      November 30, 2022
    • More
      • Privacy Policy
        • Java Helps
        • Android Apps
      • Contact US
      • About
    Facebook X (Twitter) Instagram
    Java HelpsJava Helps
    Home » What is Garbage Collection?
    Learn
    Learn
    Featured

    What is Garbage Collection?

    Gobinath LoganathanBy Gobinath LoganathanDecember 7, 2022Updated:December 7, 2022No Comments6 Mins Read
    Share
    Facebook Twitter LinkedIn Pinterest Email

    Collecting garbage is a messy business both in the real world and the programming world. Garbage collection is still one of the root causes of many bugs in low-level languages such as C. It is also a reason why modern languages like Java or Rust provide solutions to automatically handle garbages. Though modern languages do not expect every developer to be aware of memory usage and garbage collection, understanding what is garbage collection and the impact of garbage collection will help you optimize your code and Java Virtual Machine. This article introduces what is garbage collection and why it is important to understand garbage collection.

    What is Garbage?

    In C, pointers are used to access objects (C doesn’t have the concept of objects but I am using Java’s term here for Java developers to understand). In theory, developers can walk through the entire allocated memory area using pointers and access anything stored in the memory. This means that the developer can allocate memory in one function and access that allocated memory in another function. Therefore, the language cannot decide if an object is accessible to the user or not. In C, any unfreed memory allocation is considered garbage if the developer has no intention to use them later.

    On the other hand, Java employs references to reach objects. Direct memory access is kept for advanced use cases and is not something used for day-to-day tasks. Therefore, Java can determine if an object is reachable by the runtime by checking the active references to that object. Such objects without any active reference to reach are defined as garbage.


    Why Does It Matter?

    Since it is hard to explain the behavior using Java, let’s use C for a little experiment. The following C code allocates a memory location with 10,000,000,000 bytes and stores a string value repetitively in a “for” loop. Save this code as application.c on your computer.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    int main()
    {
    int i;
    for (i = 0; i < 10000; i )
    {
    // Allocate the memory
    char *name = malloc(10000000000);
    if (name == 0)
    {
    printf("ERROR: Out of memory\n");
    return 1;
    }
    // Assign a value and print it
    strcpy(name, "Hello, world!");
    printf("%s\n", name);
    }
    // Pause the application
    getchar();
    return 0;
    }

    Compile the code using any C compiler. The GNU Compiler Collection command to compile in Linux is given below.

    gcc application.c

    In Linux, it will produce an executable output. Run the following command to run the program.

    /.a.out

    Depending on your system configuration, either you may get Out of memory error or 10,000 “Hello World” messages. My computer (Linux Mint 64-bit, AMD Ryzen 7 5800X 8-Core Processor, and 32 GB memory) prints 10,000 messages. While the process is waiting for the user input before exits, you can check the memory used by the process in the system monitor. As you can see, the program takes 40.9 MB in memory. If you increase the number of iterations (in other words, the number of garbage memory allocations), you may get Memory out of error.

    C process without freeing memory allocation

    Next, modify the code as shown below. Note the free(name); on line number 21. The free command is used to free the memory allocated by malloc.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    int main()
    {
    int i;
    for (i = 0; i < 10000; i )
    {
    // Allocate the memory
    char *name = malloc(10000000000);
    if (name == 0)
    {
    printf("ERROR: Out of memory\n");
    return 1;
    }
    // Assign a value and print it
    strcpy(name, "Hello, world!");
    printf("%s\n", name);
    // Free the memory
    free(name);
    }
    // Pause the application
    getchar();
    return 0;
    }

    Compile the source code and run it again.

    This time our application consumed just 102.4 kB. This example clearly shows the impact of garbage objects on memory management. In a language that does not provide automatic garbage collection, you must be aware of what you are doing. If not, your application may throw memory out of error and terminate.

    C process after freeing memory allocation

    In a low-level language like C, the developer has the responsibility to free the memory after use. Failing to do so can crash your application. In Java, there is a dedicated daemon process to clear the garbage for you. However, nothing comes for free! The more garbage you create, the more work Java has to do behind the scene to sweep the memory. This will allocate more computing resources for garbage collection and can have a severe impact on your software’s performance. More details about detailed garbage collection algorithms will be covered in another article.


    From Object To Garbage

    The above example demonstrated garbage in C. Let’s see how an object becomes garbage in Java. To understand how an object becomes garbage, first, we need to understand how an object is created in memory. Almost all programming languages including Java use three memory regions to store runtime variables:

    • Static – To store static primitives and references
    • Stack – To store local primitives and references
    • Heap – To store objects

    Note that all objects are stored in heap doesn’t matter where it was created. For example, running the following code will create a Student object in the heap but the stu reference remains in slack. All the primitive variables and references (the Student class has only one integer primitive) will be stored inside the object.

    public class StudentDemo {
    public static void main(String[] args) {
    Student stu = new Student(10);
    }
    }

    class Student {
    int index;

    public Student(int index) {
    this.index = index;
    }
    }
    Java Object Creation in Heap

    Null Reference
    If you reassign the stu reference to null after line number 3, the Student object created in the heap will become unreachable, aka garbage.

    public class StudentDemo {
    public static void main(String[] args) {
    Student stu = new Student(10);
    stu = null;
    }
    }

    class Student {
    int index;

    public Student(int index) {
    this.index = index;
    }
    }
    what is garbage collection - Java Null Reference Garbage

    Reassigning Reference
    To make an object garbage, you don’t have to assign null to the reference. You can create a new object and use the existing reference to point to the new object and make the old one garbage. Again the idea is not much related to the reference but about if an object in the heap is reachable or not. The code given below creates a new object on line number 4 and assigns it to the existing reference. This will make the student object with index 10 garbage.

    public class StudentDemo {
    public static void main(String[] args) {
    Student stu = new Student(10);
    stu = new Student(11);
    }
    }

    class Student {
    int index;

    public Student(int index) {
    this.index = index;
    }
    }
    Java Reference Reassignment Garbage

    Island of Isolation
    To strengthen the idea that garbage is about reachability but not about having references, let’s see another example. In the following code, there are three Node objects referring to each other. However, after making all the references a, b, and c null on line numbers 14 to 16, all three Node objects become garbage.

    public class IslandOfIsolation {
    public static void main(String[] args) {
    // Create the objects
    Node a = new Node(1);
    Node b = new Node(2);
    Node c = new Node(3);

    // Create internal references
    a.next = b;
    b.next = c;
    c.next = a;

    // Remove the references from main method
    a = null;
    b = null;
    c = null;

    // Now there are 3 garbage objects
    }
    }

    class Node {
    int val;
    Node next;

    public Node(int val) {
    this.val = val;
    }
    }
    Java Island of Isolation Garbage

    What is Garbage Collection?

    Let’s end this article with a brief introduction to garbage collection in Java.

    As mentioned earlier, Java developers do not have to worry about garbage collection to the same extent as a C developer because Java takes care of garbage collection. In the Java Virtual Machine, there is a dedicated daemon thread to monitor the heap usage and to clean the heap if needed. The process of removing unreachable objects (garbage) is known as garbage collection. Though Java collects the garbage automatically, creating too many garbage objects can cause serious performance issues in your application. This may not trivial with simple Java applications like a web service. However, the usability of complex solutions like data processing, machine learning, or big data computations can be decided by garbage collection.

    The next article will cover the impact of garbage in Java Virtual Machine and the best practices to reduce the amount of garbage. I will also write another detailed article on how to tune Java Virtual Machine garbage collection algorithms for the best performance.

    Have you found this article useful? Please let me know below in the comments. Knowing someone found my articles useful motivates me to write more. Also, comment below if you face any issues with following this article or getting it working. I will try my best to help you resolve the problem. The Java Helps community is also willing to help each other and grow together.

    java learn
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email

    Related Posts

    How to Set up Scala in IntelliJ IDEA?

    December 2, 2022

    Jersey 3.x – Hello World!

    December 2, 2022

    Microservices Framework for Java (MSF4J) – Hello World!

    December 1, 2022

    How to Parse PCAP files in Java?

    November 30, 2022

    Install The Latest IntelliJ IDEA on Linux

    November 28, 2022
    Don't Miss
    Configure

    Things To Do After Installing IntelliJ IDEA

    December 20, 2022

    IntelliJ Idea: the famous IDE for JVM languages including Java, Scala, and Kotlin. If you…

    How to Set Proxy for IntelliJ IDEA?

    December 20, 2022

    What is Garbage Collection?

    December 7, 2022

    Manage GitHub Artifact Storage Quota

    December 6, 2022
    Our Picks

    Things To Do After Installing IntelliJ IDEA

    December 20, 2022

    How to Set Proxy for IntelliJ IDEA?

    December 20, 2022

    What is Garbage Collection?

    December 7, 2022

    Manage GitHub Artifact Storage Quota

    December 6, 2022
    About Us
    About Us

    Java Helps is the platform to share all about the Java ecosystem. All the sample code available on Java Helps articles are published under Apache 2.0 License. Readers are free to use them according to the Apache 2.0 License.

    "The world is my town; its people my kinsmen."
    -Kaṉiyan Pūngunṟanār

    Email Us: www.javahelps@gmail.com

    Our Picks

    Things To Do After Installing IntelliJ IDEA

    December 20, 2022

    How to Set Proxy for IntelliJ IDEA?

    December 20, 2022

    What is Garbage Collection?

    December 7, 2022
    New Comments

      Type above and press Enter to search. Press Esc to cancel.

      Go to mobile version