



























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 35
This page cannot be seen from the preview
Don't miss anything!




























adrish.b@ardentcollaboratio
246
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
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
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
adrish.b@ardentcollaboratio
252
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
adrish.b@ardentcollaboratio
255
exec( ) to launch notepad
adrish.b@ardentcollaboratio
256
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
adrish.b@ardentcollaboratio
259
adrish.b@ardentcollaboratio
261
Timing a for loop from 0 to 1,000, Elapsed time: 10
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
adrish.b@ardentcollaboratio
264
adrish.b@ardentcollaboratio
265