Runtime.gc():
- Runtime class has non static gc() method.
- It is used to start garbage collector on request.
- Runtime object representing JVM directly.
- We request the JVM directly to start Garbage collector.
/*
class Runtime
{
public void gc()
{
Runs the garbage collector.
}
}
*/
class Test
{
Test()
{
System.out.println("Created : " + this);
}
protected void finalize()
{
System.out.println("Deleted : " + this);
}
}
class GCDemo
{
public static void main(String[] args)
{
for (int i=1 ; i<=10 ; i++)
{
new Test();
Runtime jvm = Runtime.getRuntime();
jvm.gc();
try{
Thread.sleep(1000);
}catch(Exception e){ }
}
}
}
exec(String command):
- Pre-defined method belongs to Runtime class.
- It is used to execute a System command.
- JVM is also working like Virtual OS. Hence it can run OS commands as well.
class JVM
{
public static void main(String[] args) throws Exception
{
Runtime jvm = Runtime.getRuntime();
jvm.exec("notepad");
jvm.exec("mspaint");
}
}