| Lang package - Basics
|
|
Home | Lang Basics |
String | StringBuffer |
Thread |
Wrapper Class | String Tokenizer
|
|
Garpage Collection:
Process
System
Runtime
Memory Management
|
Process:
The Runtime.exec methods create a native process and return an instance of a subclass of Process that can be used to control the process
and obtain information about it. The class Process provides methods for performing input from the process, performing output to the process,
waiting for the process to complete, checking the exit status of the process, and destroying (killing) the process.
The Runtime.exec methods may not work well for special processes on certain native platforms, such as native windowing processes,
daemon processes, Win16/DOS processes on Microsoft Windows, or shell scripts. The created subprocess does not have its own terminal
or console. All its standard io (i.e. stdin, stdout, stderr) operations will be redirected to the parent process through three streams
(Process.getOutputStream(), Process.getInputStream(), Process.getErrorStream()). The parent process uses these streams to feed input
to and get output from the subprocess. Because some native platforms only provide limited buffer size for standard input and output
streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block,
and even deadlock.
|
System:
The System class contains several useful class fields and methods. It cannot be instantiated.
Among the facilities provided by the System class are standard input, standard output, and error output streams; access
to externally defined properties and environment variables; a means of loading files and libraries; and a utility method
for quickly copying a portion of an array.
|
Runtime:
Every Java application has a single instance of class Runtime that allows the application to interface with the environment
in which the application is running. The current runtime can be obtained from the getRuntime method.
An application cannot create its own instance of this class.
|
Memory Management:
Provides the management interface for monitoring and management of the Java virtual machine as well as the operating
system on which the Java virtual machine is running. It allows both local and remote monitoring and management of the
running Java virtual machine.
|
Example:
import java.lang.*;
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);
mem2 = r.freeMemory();
System.out.println("Free memory after allocation:" + mem2);
System.out.println("Memory used by allocation:" + (mem1-mem2));
for(int i=0; i<1000; i++)someints[i] = null;
r.gc();
mem2 = r.freeMemory();
System.out.println("Free memory after collecting:" + " discarded Integers: " + mem2);
}
}
Output:
Total memory is: 5177344
initial free memory: 4977384
Free memory after garbage collection: 5061496
Free memory after allocation: 5043084
Memory used by allocation: 18416
free memory after collecting: discarded integers: 5061496
|
Clone() and the Cloneable Interface:
Most of the methods defined by Object are discussed elsewhere in this book.However,one deserves special attention:clone().The clone() method
generates a duplicate copy of the object on which it is called. Only classes that implement the cloneable interface can be cloned.
The Cloneable interface defines no members.It is used to indicate that a class allows a bitwise copy of an object(that is,a clone)
to be made.If you try to call clone() on a class that does not implement Cloneable, a CloneNotSupportedException is thrown.
|
Example:
public class MyObject extends Object
{
public static void main(String[] args)
{
// Create three instance of MyObject objects.
MyObject obj1 = new MyObject("Hello");
MyObject obj2 = new MyObject("World");
MyObject obj3 = null;
try
{
obj3 = (MyObject)obj2.clone();
}
catch (CloneNotSupportedException ex)
{
System.out.println(ex.toString());
}
// Is obj1 equal to obj2?
boolean equality = obj1.equals(obj2);
System.out.println("obj1 equals obj2? " +equality);
// Is obj2 equal to obj3?
equality = obj2.equals(obj3);
System.out.println("obj2 equals obj3? " +equality);
}
public MyObject(String s)
{
aString = s;
}
protected Object clone() throws CloneNotSupportedException
{
MyObject newObj = new MyObject(aString);
return newObj;
}
// Two MyObject objects are equal if the aString
// fields are equal.
public boolean equals(Object o)
{
if (o instanceof MyObject)
{
MyObject myO = (MyObject)o;
if (aString == myO.aString)
{
return true;
}
}
return false;
}
private String aString = "";
}
Output:
obj1 equals obj2? false
obj2 equals obj3? true
|