Java Memory Management and Executing Other Programs, Slides of Java Programming

Information on java memory management and executing other programs using the runtime class. It includes details on methods like freememory(), gc(), and exec(). The document also demonstrates code snippets for memory allocation and freeing up memory, as well as executing other programs using the exec() method.

Typology: Slides

2011/2012

Uploaded on 07/07/2012

proo
proo 🇮🇳

4.4

(26)

96 documents

1 / 35

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Runtime -class
The Runtime class encapsulates the run-time
environment. We cannot instantiate a Runtime
object. However, we can get a reference to the
current Runtime object by calling the static method
Runtime.getRuntime( ).
With this reference we can call several methods that
control the state and behavior of the Java Virtual
Machine.
adrish.b@ardentcollaboratio
ns.com
http://www.ardentcollaborati
ons.com
246
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23

Partial preview of the text

Download Java Memory Management and Executing Other Programs and more Slides Java Programming in PDF only on Docsity!

Runtime -class

 The Runtime class encapsulates the run-time

environment. We cannot instantiate a Runtime

object. However, we can get a reference to the

current Runtime object by calling the static method

Runtime.getRuntime( ).

 With this reference we can call several methods that

control the state and behavior of the Java Virtual

Machine.

adrish.b@ardentcollaboratio

246

Runtime -class

Method Description void addShutdownHook(Thread thrd) Registers thrd as a thread to be run when the Java virtual machine terminates. Process exec(String progName) throws IOException

Executes the program specified by progName as a separate process. An object of type Process is returned that describes the new process. Process exec(String progName, String environment[ ]) throws IOException

Executes the program specified by progName as a separate process with the environment specified by environment. An object of type Process is returned that describes the new process.

adrish.b@ardentcollaboratio

247

Runtime -class

Method Description long freeMemory( ) Returns the approximate number of bytes of free memory available to the Java run-time system. void gc( ) Initiates garbage collection. static Runtime getRuntime( ) Returns the current Runtime object. void halt(int code) Immediately terminates the Java virtual machine. No termination threads or finalizers are run. The value of code is returned to the invoking process. void load(String libraryFileName) Loads the dynamic library whose file is specified by libraryFileName, which must specify its complete path.

adrish.b@ardentcollaboratio

249

Runtime -class

Method Description void loadLibrary(String libraryName) Loads the dynamic library whose name is associated with libraryName. boolean removeShutdownHook(Thread thrd)

Removes thrd from the list of threads to run when the Java virtual machine terminates. It returns true if successful that is, if the thread was removed. void runFinalization( ) Initiates calls to the finalize( ) methods of unused but not yet recycled objects. long totalMemory( ) Returns the total number of bytes of memory available to the program.

adrish.b@ardentcollaboratio

250

Memory Management

Java has the feature of automatic garbage collection , then

also sometimes we need to know how large the object heap

is and how much of it is left.

Sometimes we also need to run garbage collection on

Demand to free up the memory.

adrish.b@ardentcollaboratio

252

Memory Management

Showing memory operations using Runtime class..using totalMemory() ,freeMemory() and gc()

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);

adrish.b@ardentcollaboratio

253

Executing Other Programs

 In safe environments, you can use Java to execute

other heavyweight processes (that is,programs) on

your multitasking operating system. Several forms of

the exec( ) method allow you to name the program

you want to run as well as its input parameters. The

exec( ) method returns a Process object, which can

then be used to control how your Java program

interacts with this new running process. Because Java

can run on a variety of platforms and under a variety

of operating systems, exec( ) is inherently

environment-dependent.

adrish.b@ardentcollaboratio

255

Executing Other Programs

exec( ) to launch notepad

class ExecDemo {

public static void main(String args[]) {

Runtime r = Runtime.getRuntime();

Process p = null;

try {

p = r.exec("notepad");

} catch (Exception e) {

System.out.println("Error executing notepad.");

adrish.b@ardentcollaboratio

256

Executing Other Programs

class ExecDemoFini { public static void main(String args[]) { Runtime r = Runtime.getRuntime(); Process p = null; try { p = r.exec("notepad"); p.waitFor(); } catch (Exception e) { System.out.println("Error executing notepad."); } System.out.println("Notepad returned " + p.exitValue()); } }

adrish.b@ardentcollaboratio

258

System

The System class holds a collection of static

methods and variables. The standard input,

output, and error output of the Java run time are

stored in the in, out, and err variables.

adrish.b@ardentcollaboratio

259

currentTimeMillis( )

 class Elapsed {

 public static void main(String args[]) {

 long start, end;

 System.out.println("Timing a for loop from 0 to 1,000,000");

 // time a for loop from 0 to 1,000,

 start = System.currentTimeMillis(); // get starting time

 for(int i=0; i < 1000000; i++) ;

 end = System.currentTimeMillis(); // get ending time

 System.out.println("Elapsed time: " + (end-start));

adrish.b@ardentcollaboratio

261

Timing a for loop from 0 to 1,000, Elapsed time: 10

arraycopy( )

 class ACDemo {  static byte a[] = { 65, 66, 67, 68, 69, 70, 71, 72, 73, 74 };  static byte b[] = { 77, 77, 77, 77, 77, 77, 77, 77, 77, 77 };  public static void main(String args[]) {  System.out.println("a = " + new String(a));  System.out.println("b = " + new String(b));  System.arraycopy(a, 0, b, 0, a.length);  System.out.println("a = " + new String(a));  System.out.println("b = " + new String(b));  System.arraycopy(a, 0, a, 1, a.length - 1);  System.arraycopy(b, 1, b, 0, b.length - 1);  System.out.println("a = " + new String(a));  System.out.println("b = " + new String(b));  }  } adrish.b@ardentcollaboratio

262

a = ABCDEFGHIJ b = MMMMMMMMMM a = ABCDEFGHIJ b = ABCDEFGHIJ a = AABCDEFGHI b = BCDEFGHIJJ

Class-class

 Impotant methods

 forName()

 getClass()

 boolean isInterface( )

 String getName( )

adrish.b@ardentcollaboratio

264

Class-class

 class X {

 int a;

 float b;

 class Y extends X {

 double c;

 class RTTI {

 public static void main(String args[]) {

 X x = new X();

 Y y = new Y();

adrish.b@ardentcollaboratio

265