Java.lang - SPLessons

Java.lang Runtime Class

Home > Lesson > Chapter 10
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.lang Runtime Class

java.lang Runtime Class

shape Description

The java.lang.Runtime class permits the application to interface with nature in which the application is running. Following is the syntax declaration of java.lang Runtime class. [java] public class Runtime extends Object [/java] Java Runtime class gives techniques to execute a procedure, conjure GC, get aggregate and free memory and so on. There is one and only occurrence of java.lang.Runtime class is accessible for one application. java.lang Runtime Class inherit the properties from java.lang.Object class.

Methods of java.lang Runtime Class

shape Methods

Following are the common using methods of java.lang Runtime Class.
Methods Description
void addShutdownHook(Thread hook) To register new hook thread.
public long freeMemory() To return the free memory in JVM.
gc() To run the garbage collector.
totalMemory() To return the free memory in JVM.
loadLibrary(String libname) To stack the dynamic library with the predefined library name.

shape Example

By using java.lang.Runtime.addShutdownHook(Thread hook)

DemoRuntime.java [java] package com.SPlessons; public class DemoRuntime { // a class that extends thread that is to be called when program is exiting static class Message extends Thread { public void run() { System.out.println("Bye-Bye."); } } public static void main(String[] args) { try { // register Message as shutdown hook Runtime.getRuntime().addShutdownHook(new Message()); // print the state of the program System.out.println("Here Program is starting..."); // cause thread to sleep for 3 seconds System.out.println("Here Waiting for 3 seconds..."); Thread.sleep(3000); // print that the program is closing System.out.println("Program is closing..."); } catch (Exception e) { e.printStackTrace(); } } } [/java] The run() strategy gives a passage indicate the string and developer will put the finish business rationale inside this technique. To return the singleton instance Runtime.getRuntime() method will be used. [java] public void run()[/java] Output: When compile the code result will be as follows. [java] Here Program is starting... Here Waiting for 3 seconds... Program is closing... Bye-Bye. [/java]

By using java.lang.Runtime.freeMemory() method

[java] package com.SPlessons; public class DemoRuntime { public static void main(String[] args) { // print when the program starts System.out.println("Here Program is starting..."); // print the number of free bytes System.out.println("" + Runtime.getRuntime().freeMemory()); } } [/java] The functionality of java.lang.Runtime.freeMemory() method is to return free memory in JVM. Following is the syntax declaration. [java]public long freeMemory()[/java] Output: When compile the code result will be as follows. [java] Here Program is starting... 15796032 [/java]

By using java.lang.Runtime.gc() method

[java]package com.SPlessons; public class DemoRuntime { public static void main(String[] args) { // print when the program starts System.out.println("Here Program is starting..."); // run the garbage collector System.out.println("Running Garbage Collector..."); Runtime.getRuntime().gc(); System.out.println("Completed."); } } [/java] The garbage collector is used to remove the unused objects in the code, to free up the space. Following is the syntax declaration of the method. [java]public void gc()[/java] Output: Now compile the code result will be as follows. [java] Here Program is starting... Running Garbage Collector... Completed. [/java]

By using java.lang.Runtime.totalMemory() method

[java] package com.SPlessons; public class DemoRuntime { public static void main(String[] args) { // print the state of the program System.out.println("Here Program is starting..."); // print the total memory System.out.println("" + Runtime.getRuntime().totalMemory()); } } [/java] Output: When compile the code result will be as follows. [java]Here Program is starting... 16252928 [/java]

By using java.lang.Runtime.loadLibrary(String filename) method

[java] package com.SPlessons; public class DemoRuntime { public static void main(String[] args) { // print when the program starts System.out.println("Here program is starting..."); // load a library that is Windows/System32 folder System.out.println("Here loading the library..."); Runtime.getRuntime().loadLibrary("C:/Windows/System32/crypt32.dll"); System.out.println("Library Loaded."); } } [/java] This strategy stacks the dynamic library with the predetermined library name. A document containing local code is stacked from the neighborhood record framework from a place where library documents are ordinarily acquired. The points of interest of this procedure are usage subordinate. The mapping from a library name to a particular filename is done in a framework particular way. Following is the syntax declaration of java.lang.Runtime.loadLibrary(String filename) method. [java]public void loadLibrary(String libname)[/java] Output: When compile the code result will be as follows. [java] Here program is starting... Here loading the library... Library Loaded.[/java]

Summary

shape Key Points

  • To return the singleton instance of run time class Runtime.getRuntime() will be used.
  • To terminate the present virtual machine public void exit(int status) will be used.
  • To return the number of processors public int availableProcessors() method will be used.