Home Java Docker
Home     Java

Java Topic

What is Java
History of Java
Freature of Java
Difference Between Java & C++
Java Environment Set Up
Java Hello World Program & its Internal Process
Java Hello World Program
JDK, JRE and JVM
Java Variables
Java Data Types & Unicode System
Java Operators
Java Keywords
Java Control Statements
Java if else
Java switch
Java for loop
Java While loop
Java Do While loop
Java break
Java continue
Java Oops Concept
Java Object & Class
Java Method
Java Constructor
Java Static Keyword
Java this Keyword
Java Inheritance
Java Hybrid Inheritance
Aggregation(HAS-A)
Java Polymorphism
Java method overloading
Java method overriding
Java Runtime polymorphism
Java Dynamic Binding
Super keyword
Final keyword
Difference Between method overloading and method overriding
Java Abstraction
Java Interface
Abstract class vs Interface
Java Encapsulation
Java Package
Java Access Modifiers
covariant return type
Instance initializer block
Java instanceof operator
Object Cloning in Java
Wrapper classes in Java
Java Strictfp Keyword
Recursion in Java
Java Command Line Arguments
Difference between object and class
Java String
Java String Class
Java Immutable String
Java Immutable Class
String Buffer
String Builder
String Buffer vs String
String Builder vs String Buffer
String Tokenizer in Java
Java Array
Java Exceptions Handling
Java Try-Catch block
Java Multiply Catch Block
Java Finally Block
Java Throws Keyword
Java Throw Keyword
Java Exception Propagation
Java Throw vs Throws
Final vs Finally vs Finalize
Exception Handling With Method Overridding
Java Multithreading
Lifecycle and States of a Thread in Java
How to create a thread in Java
Thread Scheduler in Java
Sleeping a thread in Java
Calling run() method
Joining a thread in Java
Naming a thread in Java
Thread Priority
Daemon Thread
Thread Pool
Thread Group
Shutdown hook
Multitasking vs Multithreading
Garbage Collection
RunTime Class
Java Synchronization
Synchronized block in Java
Static Synchronization in Java
Deadlock in Java
Inter Thread Communication in Java
Interrupting Thread in Java
Reentrant Monitor in Java
Java Applet
Animation in Applet
EventHandling in Applet
Display image in Applet
Displaying Graphics in Applet
Parameter in Applet
Java 8 Features
Java Lambda Expressions
Method References
Functional Interfaces
Java 8 Stream
Base64 Encode Decode
Default Method
for Each() Method
Collectors class
String Joiner Class
Optional Class
JavaScript Nashron
Parallel Array Sort
Type Interface
Parameter Reflection
Type and Repeating Annotations
JDBC Improvements

Garbage Collection in Java

  • Java programs automatically manage their memory through a process known as garbage collection. Java programs can be executed on a Java Virtual Machine, or JVM, by compiling to bytecode. Objects are created on the heap, a section of memory reserved for the program, when Java programs are run on the JVM. Some things will eventually become obsolete. To free up memory, the garbage collector discovers these unused objects and deletes them.
    • A programmer is in charge of both creating and destroying objects in C/C++. Programmers frequently forget to destroy useless objects. This carelessness may eventually prevent enough memory from being available to create new objects, resulting in an abnormal program termination and OutOfMemoryErrors.




  • The programmer doesn't need to be concerned about unused objects in Java as they are automatically handled by the garbage collector. The job of the garbage collector is to make free memory space by destroying objects that are no longer accessible. The garbage collector can be likened to the Daemon thread as it continually operates in the background.
  • Java employs an automatic garbage collection system, which involves scanning the heap memory to identify objects that are actively used and those that are not. The unused objects are then deleted. An object is considered in use or referenced if a part of the program still holds a pointer to it.
  • The memory that an object has taken up can be freed up when it is no longer referenced or used by any part of the programme.Programmers do not need to explicitly mark objects for deletion because the Java Virtual Machine (JVM) automatically takes care of garbage collection.




How does an object become unreferenced?


1.By nulling a reference

    Student stu = new Student();  
    stu=null;  

2.By assigning a reference to another

    Student stu1 = new Student();  
    Student stu2 = new Student();  
    stu1=stu2;  

Java garbage collection example

public class Main{
    public void finalize(){
    	System.out.println("Garbage Collected");
    }
    public static void main(String arg[]) {
    	Main main1=new Main();  
    	Main main2=new Main();  
    	main1=null;  
    	main2=null;  
    	System.gc();  
   }
}



Output:

Garbage Collected
Garbage Collected 


Important point of Garbage collection

  • Garbage collection (GC) in Java is handled by a daemon thread known as the Garbage Collector. The finalise() method is used by this thread just before an object is garbage collected.
  • The JVM's garbage collector only remove objects that were created with the "new" keyword. If any objects were created without using "new," the finalize() method can be utilized to perform cleanup operations and destroy those remaining objects.
  • Each time an object becomes eligible for garbage collection, the finalize() method is called, providing an opportunity for cleanup processing. This method is defined in the Object class.



  • To initiate the garbage collection process, the gc() method is invoked. This method is present in the System and Runtime classes and is responsible for triggering cleanup operations.


Types of Java Garbage Collection Activities


In Java, two types of garbage collection activity are common as given below

  • Garbage collection that is minor or incremental : It is thought to have happened when unreachable objects in young generation heap memory were removed.
  • Garbage Collection, Major or Complete : It is said to have happened when the objects that survived the minor garbage collection were copied into the old generation or permanent generation heap memory and then removed. Garbage collection occurs less frequently in the elderly than in the younger generation.



RunTime Class in Java Next »
« Perv Next »


Post your comment





Read Next Topic
Java Tutorial - Topic
Java Multithreading
Lifecycle and States of a Thread in Java
How to create a thread in Java
Thread Scheduler in Java
Sleeping a thread in Java
Calling run() method
Joining a thread in Java
Naming a thread in Java
Thread Priority
Daemon Thread
Thread Pool
Thread Group
Shutdown hook
Multitasking vs Multithreading
Garbage Collection
RunTime Class

Read Other Java Chapter
Java Topic
Java Basic Tutorial
Java Control Statements
Java Classes & Object
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java OOPs Miscellaneous
Java Array
Java String
Java Exception Handling
Java Multithreading
Java Synchronization
Java Applet
Java 8 Features
Java 9 Features
Java Collection
Java Mcq
Java Interview Question
Tools
  

Useful Links

  • Home
  • Blog
  • About us
  • Contact Us
  • Privacy policy

Contact Us

Police Colony
Patna, Bihar
India

Email:

About DockerTpoint


India's largest site for Programming Tutorial as well as BANK, SSC, RAILWAY exam
and Campus placement preparation.