Memory Management

  • As we know Java provides Garbage collection although there is a need to get how large the total heap is and how much in it is left by using two methods called totalMemory() and freeMemory().
  • sometimes we want to collect discarded objects prior to the collector’s next appointed rounds. This is done by calling gc() method to run garbage collector on demand.

  • To get a baseline memory usage call gc( )and then call freeMemory( ). and then call freeMemory( ) again to see how much memory it is allocating.

// Demonstrate totalMemory(), freeMemory() and gc().
public class MemoryDemo
{
 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); // allocate integers

 mem2 = r.freeMemory();
 System.out.println("Free memory after allocation: " + mem2);
 System.out.println("Memory used by allocation: " + (mem1-mem2));

 // discard Integers
 for(int i=0; i<1000; i++) 
  someints[i] = null;
 r.gc(); // request garbage collection

 mem2 = r.freeMemory();
 System.out.println("Free memory after collecting" + " discarded Integers: " + mem2);
 }
}

Output:

Total memory is: 16252928
Initial free memory: 15559584
Free memory after garbage collection: 15956480
Free memory after allocation: 15872576
Memory used by allocation: 83904
Free memory after collecting discarded Integers: 15956824

Examples

// Demonstrate exec().
public class ExecDemo {
 public static void main(String args[]) 
 {
 Runtime r = Runtime.getRuntime();
 Process p = null;
 try 
 {
 p = r.exec("notepad"); //returns Process object which is used to control how Java program interacts with this new running process
 } 
 catch (Exception e) 
 {
 System.out.println("Error executing notepad.");
 }
 }
}
// Wait until notepad is terminated.
public class ExecDemoFini
 {
 public static void main(String args[]) 
 {
 Runtime r = Runtime.getRuntime();
 Process p = null;
 try 
 {
 p = r.exec("notepad");
 p.waitFor(); //causes your program to wait until the sub process finishes. we can destroy sub processes by using destroy() method
 }
  catch (Exception e) 
 {
 System.out.println("Error executing notepad.");
 }
 System.out.println("Notepad returned " + p.exitValue());//returns the value returned by the sub process when it is finished. 
 //This is typically 0 if no problems occur
 }
}

ProcessBuilder

  • This provides a way to start and manage processes.
  • As we know all processes are represented by the Process class, and a process can be started by Runtime.exec( ).
  • ProcessBuilder offers more control over the processes.

  • It defines following constructors.

ProcessBuilder(List args) //arguments are passed as list

ProccessBuilder(String ... args) //arguments are passed as varargs

  • args is a list of arguments that specify the name of the program to be executed along with any required command-line arguments.

  • ProcessBuilder.Redirect class defines the methods which encapsulates an I/O source or target linked to a subprocess.

static ProcessBuilder.Redirect to(File f )//it will redirect to a file specified by the argument passed to it as File object

static ProcessBuilder.Redirect from(File f )//it will redirect from a file specified by the argument

static ProcessBuilder.Redirect appendTo(File f )//it will append to a file

File file( )//returns the File object that linked with the file.

ProcessBuilder.Redirect.Type //returns the enumeration which describes the type of the redirection. It defines values as APPEND, INHERIT, PIPE, READ, or WRITE.

  • ProcessBuilder.Redirect also defines the constants INHERIT and PIPE.
public class PBDemo 
{
 public static void main(String args[]) 
 {
 try {
 ProcessBuilder proc = new ProcessBuilder("notepad.exe", "testfile"); //creating process by simply instantiate the ProcessBuilder class
 proc.start(); //to begin the execution of a program call start()
 } 
 catch (Exception e)
  {
 System.out.println("Error executing notepad.");
 }
 }
}

results matching ""

    No results matching ""