View Single Post
  #3 (permalink)  
Old 02-08-2008, 11:25 PM
rlhs76 rlhs76 is offline
Member
 
Join Date: Feb 2008
Posts: 2
rlhs76 is on a distinguished road
Quote:
Originally Posted by tim View Post
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.

I don't write java code. I'm just a user. I have 3 pc's at home and they all have the same issue, when I increase java memory with the commmand -Xmx190m java will not load as I get a "can't load java environment" error. Maybe I posted my question in the wrong location. But I would really like to fix the problem. The application I'm running is a sitemap generator. Feel free to try it and see if it works for you. I'm running it from Firewall Test, Web Tools and Free Internet Security Audit Select the sitemap generator and generate a map from a large site. You can use mine if you like STUFF U NEED
Reply With Quote