View Single Post
  #2 (permalink)  
Old 02-08-2008, 10:51 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
Welcome, rlhs76, to Java Forums!

It is true that the Java Virtual Machine does memory management automatically, but it does so in its own time. If you write an application that uses memory heavily, you might want to free it as soon as you can to prevent the "out of memory" exception. My solution to this is to force garbage collection. You must also make sure that objects that are to be deleted are not referenced. Here is an interesting program that demonstrates my point:
Code:
package pack; import java.util.*; public class Test{ private Vector<Vector<Byte>> junk; public static void printMemory(){ Runtime runtime = Runtime.getRuntime(); System.out.println("Free memory = " + runtime.freeMemory()); System.out.println("Total memory = " + runtime.totalMemory()); } public static Vector<Byte> createJunk(int bytes){ Vector<Byte> result = new Vector<Byte>(); for (int i = 0; i < bytes; i++){ result.add(new Byte((byte)255)); } return result; } public Test(){ System.out.println("This program demonstrates memory management in java."); System.out.print("Enter the number of runs: "); int runs = Integer.parseInt(readln()); System.out.print("Use memory management (Y/N): "); boolean manageMemory = readln().toLowerCase().equals("y"); System.out.print("Enter the number bytes for each peace of junk: "); int junkSize = Integer.parseInt(readln()); run(runs, manageMemory, junkSize); } public void run(int count, boolean manageMemory, int junkSize){ try{ junk = new Vector<Vector<Byte>>(); System.out.println("Running with memory management = " + manageMemory + " :\n"); Runtime runtime = Runtime.getRuntime(); for (int i = 1; i <= count; i++){ System.out.println("run = " + i); printMemory(); System.out.println("creating junk"); Vector<Byte> someData = createJunk(junkSize); printMemory(); if (manageMemory){ try{ System.out.println("collecting"); someData = null; runtime.gc(); System.out.println("done collecting"); printMemory(); } catch (Throwable e){ showError(e); } } else { junk.add(someData); } System.out.println(); } } catch (Throwable e){ showError(e); } } public static void showError(Throwable e){ StackTraceElement[] stack = e.getStackTrace(); String stackOut = new String(); int number = 0; for (StackTraceElement element : stack) { number++; stackOut += number + "\n" + " Method: " + element.getMethodName() + "\n" + " Class: " + element.getClassName() + "\n" + " Line: " + element.getLineNumber() + "\n"; } String message = "The following problem has accured:" + "\n\n" + "Name: \n" + e.getClass().getName() + "\n\n" + "Message: \n" + e.getMessage() + "\n\n" + "Cause: \n" + (e.getCause() != null ? e.getCause().toString() : "unknown") + "\n\n" + "Stack trace: \n" + stackOut; System.out.println(message); } public static String readln(){ Scanner scanner = new Scanner(System.in); return scanner.nextLine(); } public static void main(String[] arg){ Test test = new Test(); System.out.println("Program finnished"); readln(); } }
I've attached some output for this program in text files. You can see the difference between using memory management (Good.txt) and not (Bad.txt).

I hope this will help you.
Attached Files
File Type: txt Good.txt (2.2 KB, 3 views)
File Type: txt Bad.txt (1.4 KB, 3 views)
__________________
If your ship has not come in yet then build a lighthouse.
Reply With Quote