
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java System gc() Method
Description
The Java System gc() method runs the garbage collector. Calling this suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse.
Declaration
Following is the declaration for java.lang.System.gc() method
public static void gc()
Parameters
NA
Return Value
This method does not return any value.
Exception
NA
Example: Running Garbage Collector
The following example shows the usage of Java System gc() method. In this program, we've created two arrays and initialized them. Using arraycopy() method, first element of an array is copied to second array and array 2 is printed. Now using System.gc(), we've instructed JVM to run the garbage collector to clean up the memory.
package com.tutorialspoint; public class SystemDemo { public static void main(String[] args) { int arr1[] = { 0, 1, 2, 3, 4, 5 }; int arr2[] = { 0, 10, 20, 30, 40, 50 }; // copies an array from the specified source array System.arraycopy(arr1, 0, arr2, 0, 1); System.out.print("array2 = "); for(int i= 0; i < arr2.length; i++) { System.out.print(arr2[i] + " "); } // it runs the GarbageCollector System.gc(); System.out.println("Cleanup completed..."); } }
Output
Let us compile and run the above program, this will produce the following result −
array2 = 0 10 20 30 40 50 Cleanup completed...
java_lang_system.htm
Advertisements