How To Use Garbage Collector On Java And Free The Memory [ Jeclipse Course 3 ]
What Will I Learn?
Hello,in this tutorial;
- You will learn how to use garbage collector method on java
- You will learn how to make a program that prints out your free memory
- You will learn using best methods for your program on java with using Ide.
Requirements
- Basic knowledge of coding and java
- Eclipse
- Basic knowledge on what is integrated development environment and how to use with Eclipse on Java
Difficulty
- Basic
Tutorial Contents
Hello,in this tutorial i am going to show you guys how to use garbage collector (gc) and clean unused materials on java.Let's get started...
I don't have to define library for this project.I'm defining class and starting my program.
public class Memory
{
public static void main (String args[])
{
Now i am defining "runtime" as r "runtime" is all java applications has a single instance of class runtime that allows the application to interface with the environment in which the application is running,for that reason we need "runtime" in our program.
After that i am using "get.runtime" method.This method,returns the runtime object associated with the current java app.Then i define two long type variables named as "mem1,mem2".In the below line i create a new integer array which could have one thousand members in.
Runtime r = Runtime.getRuntime();
long mem1,mem2;
Integer someints[] = new Integer[1000];
In this step i will make program print out how much memory does java virtual machine has.For that i need to use my "r" which we defined as "runtime" at above then use the "totalmemory" method.This method returns the total amount of memory in the java virtual machine.
System.out.println("Total memory is :" + r.totalMemory());
After that we equalize our "long" variable "mem1" to "r"."freememory".Freememory method returns the amount of free memory in the java virtual machine.Then we make program to print out and show user the initial free memory and add it on "mem1" variable.
mem1 = r.freeMemory();
System.out.println("Initial free memory" + mem1);
Now one of the most important parts of code is here,we are going to run "garbage collector" in this step."Garbage collector(gc)" runs the garbage collector. Calling this method 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.
When control returns from the method call, the virtual machine has made its best effort to recycle all discarded objects.This method is also important because it will make java work faster because it deletes all unused objects on java virtual machine that you created or used before.
r.gc();
Now after running the garbage collector we will make system to show user what is the current free memory after the garbage collection and add it on "mem1" variable again.
mem1 = r.freeMemory();
System.out.println("Free memory after garbage collection:" + mem1);
I am going to use "for" loop now to create random integers and use "someints" integer to create these.After we use our second "long" type varible "mem2" to print out the current free memory after the allocation and add it on "mem2".
for(int i=0; i<1000; i++)
someints[i] = new Integer(i);
mem2 = r.freeMemory();
System.out.println("Free memory after allocation:" + mem2);
In this step we make system print out how much memory allocation is used. We need to subtract "mem1-mem2" and add the result on current free memory.
System.out.println("Memory used by allocation:" + (mem1-mem2));
for(int i=0; i<1000; i++) someints[i] = null;
In the final step we use garbage collector again then equalize mem2 to freememory.As i told before freememory method will return the free amount in java virtual machine and we equalize it to "mem2" and in the end of the code we make the system print out the final memory after collecting the discarded integers and add it on to "mem2".
Please do not forget that using garbage collector is really important for your programs and for your professionality.It allows you to create new methods easier and delete old unused objects.
r.gc();
mem2 = r.freeMemory();
System.out.println("Free memory after collecting " + "discarded integers:" + mem2);
Whole Code
public class Memory
{
public static void main (String args[])
{
Runtime r = Runtime.getRuntime();
long mem1,mem2;
Integer someints[] = new Integer[1000];
System.out.println("Total memory is :" + r.totalMemory());
mem1 = r.freeMemory();
System.out.println("Initial free memory" + mem1);
r.gc();
mem1 = r.freeMemory();
System.out.println("Free memory after garbage collection:" + mem1);
for(int i=0; i<1000; i++)
someints[i] = new Integer(i);
mem2 = r.freeMemory();
System.out.println("Free memory after allocation:" + mem2);
System.out.println("Memory used by allocation:" + (mem1-mem2));
for(int i=0; i<1000; i++) someints[i] = null;
r.gc();
mem2 = r.freeMemory();
System.out.println("Free memory after collecting " + "discarded integers:" + mem2);
}
}
Output
Curriculum
Posted on Utopian.io - Rewarding Open Source Contributors
Your contribution cannot be approved because it does not follow the Utopian Rules, and is considered as plagiarism. Plagiarism is not allowed on Utopian, and posts that engage in plagiarism will be flagged and hidden forever.
Original tutorial
You can contact us on Discord.
[utopian-moderator]
Thank you for the warning,i didn't know that i couldn't take the code from source and tell people what does the code means.In the next tutorials i will write the program.
Nice try.