Results 1 to 3 of 3
  1. #1
    rlhs76 is offline Member
    Join Date
    Feb 2008
    Posts
    2
    Rep Power
    0

    Default Low memory error

    When I run an application (this one creates a site map) I was getting a low memory error. So to increase the java memory, I went to the java console and entered -Xmx128m. Still getting the error I increased the memory size up to 185. Still get the low memory error. When I enter a value of -Xmx190 or higher, I get something like "can't run/start java environment" when I run the application.

    What is my problem and how can I fix it. I have 1Gb ram on this computer. This is the newest java download.

  2. #2
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    8

    Default

    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:
    Java 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). :eek:

    I hope this will help you. ;)
    Attached Files Attached Files
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  3. #3
    rlhs76 is offline Member
    Join Date
    Feb 2008
    Posts
    2
    Rep Power
    0

    Default

    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:
    Java 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). :eek:

    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

Similar Threads

  1. Out of memory
    By mew in forum New To Java
    Replies: 1
    Last Post: 01-20-2008, 09:55 AM
  2. Memory
    By mew in forum CLDC and MIDP
    Replies: 0
    Last Post: 12-28-2007, 12:02 PM
  3. How to reduce the size or avoiding out of memory error?
    By rajeshkumarmsc in forum Advanced Java
    Replies: 3
    Last Post: 08-11-2007, 11:15 PM
  4. Java Heap Out of Memory Error
    By stonkers in forum New To Java
    Replies: 3
    Last Post: 07-17-2007, 05:43 PM
  5. Consumption of memory
    By Daniel in forum Advanced Java
    Replies: 1
    Last Post: 07-06-2007, 10:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •