Updates
  • Starting New Weekday Batch for Java Full Stack Developer on 24th June 2024 @ 02:00 PM to 04:00 PM
  • Starting New Weekend Batch for Java Full Stack Developer on 06th July 2024 @ 11:00 AM to 02:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 08th July 2024 @ 04:00 PM to 06:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 29th July 2024 @ 10:00 AM to 12:00 PM
Join Course

Java.lang.Runtime

• Runtime class is a built-in class which is available in java.lang package.
• java.lang.Runtime class default constructor has private access modifier, so we cannot instantiate outside from the Runtime class.
• Java.lang.Runtime class has been developed on Singleton Design pattern.

Singleton Design Pattern: - Singleton Design Pattern offers to create a class with a restriction on numbers of object creation.
As we know that when we define a class then we can create multiple objects using new operator. However, the number of objects created in a class that is designed using Singleton Design Pattern can be controlled by the programmer.

            
class JTC{
	public static void main(String arg[]){
		
		// A obj = new A();
		
		A a1 = A.getA();
		A a2 = A.getA();
		A a3 = A.getA();

		System.out.println("a1 hashcode :- "+a1.hashCode());
		System.out.println("a2 hashcode :- "+a2.hashCode());
		System.out.println("a3 hashcode :- "+a3.hashCode());

		System.out.println("a1 == a2 :- "+(a1 == a2));
		System.out.println("a2 == a3 :- "+(a2 == a3));
		System.out.println("a1 == a3 :- "+(a1 == a3));

	}
}
class A{
	
	static A a1 = null;
	
	private A(){

	}
	
	static{
		a1 = new A();
	}

	static A getA(){
		return a1;
	}
}
    a1 hashcode :- 1652149987
    a2 hashcode :- 1652149987
    a3 hashcode :- 1652149987
    a1 == a2 :- true
    a2 == a3 :- true
    a1 == a3 :- true
            

In this example we can see that we have class A and default constructor A() that has private access modifier, so we can’t create Student class object from class JTC.
In class A as we can see we have a static type reference variable (static A a1 = null;) and in static block we are creating an object of class A and storing their reference into a1 reference variable (a1 = new A()). And finally we have a static method getA() which returns A class object to their caller.
We have declared static type reference variable because as we know that static variable gets memory only once. Thereafter we are creating an object inside the static block because we want to create only one object and this we can do only in static block since static block executes only once. Finally we are creating a static method getA() which returns the object of class A that we have created in static block.

• Some important members of java.lang.Runtime class.: -

1. public static java.lang.Runtime getRuntime();: -

Access Modifier: - public
Member Type: - Static Method
Return Type: - java.lang.Runtime
Method Name: - getRuntime()
Parameter: - No-Parameter
Functionality: - As we know java.lang.Runtime class is designed on Singleton Design Pattern, so we cannot create their object from other class, here we can use getRuntime() method. This method returns same object of java.lang.Runtime class at every invocation that can be used to access java.lang.Runtime class members as per the requirement.

            
class JTC{
	public static void main(String arg[]){

		// Runtime obj = new Runtime();
		
		Runtime r1 = Runtime.getRuntime();
		System.out.println("r1 hashcode :- "+r1.hashCode());
		
		Runtime r2 = Runtime.getRuntime();
		System.out.println("r2 hashcode :- "+r2.hashCode());

		System.out.println("r1 == r2 :- "+(r1 == r2));
		
	}
}
    r1 hashcode :- 1691875296
    r2 hashcode :- 1691875296
    r1 == r2 :- true
            

In this example there is a class which is class JTC and inside the JTC class first we are trying to create an object of Runtime class using new operator, but as we discussed Runtime class is designed on Singleton Design pattern and so their default constructor has private access modifier, that is the reason we get an error at compile-time.

In the next section we are using getRuntime() method to get object of java.lang.Runtime class. As we know that getRuntime() method returns same object at every invocation, that is the reason the hash codes of r1 and r2 are same and when we compare r1 and r2 using == operator we get true as result.

2. public native int availableProcessors();: -

Access Modifier: - public
Member Type: - Instance method
Return Type: - int
Method Name: - availableProcessors()
Parameter: - No-Parameter
Functionality: - It returns the number of processors which are dedicated for the invocation of the current Java Virtual Machine. For different executions of Java Virtual Machine the method can be modified; it should be noted here that the maximum number of processors made available to the Java Virtual machine cannot be a zero(0) or a negative (-ve) value.

            
class JTC{
	public static void main(String arg[]){
	
    System.out.println("Numbers of Processor allocated to the current JVM processing :- ");
    System.out.println(Runtime.getRuntime().availableProcessors());}
}
    Numbers of Processor allocated to the current JVM processing :-
    4
            

In this example we are using two methods of java.lang.Runtime class which are getRuntime() and availableProcessors().Here we can see that first we are invoking Runtime.getRuntime() which returns an object of java.lang.Runtime class and using that object we are invoking availableProcessors() method to get the number of processors allocated for the current JVM processing.

3. public native long freeMemory(); :-

Access Modifier: - public
Member Type: - Instance Method
Return Type: - long
Method Name: - freeMemory
Functionality: - It returns the free heap memory and after the Garbage Collection process it increases the return value of freeMemory method.

            
class JTC{
	public static void main(String arg[]){
		
		System.out.println("Unused Heap Memory :- "+Runtime.getRuntime().freeMemory());

	}
}
    Unused Heap Memory :- 125810232
            

In the above example we are invoking Runtime.getRuntime() which returns an object of java.lang.Runtime class and using that same object we are invoking freeMemory() method which returns unused heap memory. Different invocations of freeMethod()may return different values. Here the memory measures are recorded in bytes.

4. public native long totalMemory(); :-

Access Modifier: - public
Member Type: - Instance Method
Return Type: - long
Method Name: - totalMemory()
Parameter: - totalMemory()
Functionality: - It returns the total amount of memory allocated to Heap, allocated memory can vary on the basis of hosting environment. Here the memory measures are recorded in Bytes.

            
class JTC{
	public static void main(String arg[]){
    System.out.println("Total Heap Memory :- "+Runtime.getRuntime().totalMemory());

	}
}
    Total Heap Memory :- 134217728
            

In this example we are invoking totalMemory() method of java.lang.Runtime class which returns the total amount of memory allocated to Heap area in bytes, the same method returns different values on different hosting environments.

5. public native long maxMemory();: -

Access Modifier: - public
Member Type: - Instance Method
Return Type: - long
Method Name: - maxMemory
Functionality: - it returns total amount of memory in bytes that JVM can use in future as Heap area. If there is no inherited limit then it returns the maximum value of Long primitive data type which is equivalent to 2^63-1.

            
class JTC{
	public static void main(String arg[]){
		
		System.out.println("Maximum limit of Heap  :- "+Runtime.getRuntime().maxMemory());
	}
}
    Maximum limit of Heap  :- 2118123520
            
6. public java.lang.Process exec(java.lang.String) throws java.io.IOException; :-

Access Modifier: - public
Member Type: - Instance Method
Return Type: - java.lang.Process
Method Name: - exec()
Parameter: - java.lang.String
Functionality: - It is dedicated to run a command in a separate process on hosting environment. It is same as run a command on cmd.

            
class JTC{
	public static void main(String arg[]) throws Exception{
		
		// Dedicated to open mspaint.

		Runtime.getRuntime().exec("mspaint");

		// Dedicated to open calculator.

		Runtime.getRuntime().exec("calc");

		// Dedicated to open Notepad.

		Runtime.getRuntime().exec("notepad");

	}
}

In this example we have a class which is JTC and inside the JTC class we are using exec() method of java.lang.Runtime class to open mspaint, (exec(“mspaint”)), notepad (exec(“notepad”)) and calculator (exec(“calc”)).