Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-08-2008, 04:33 PM
Member
 
Join Date: Feb 2008
Posts: 2
rlhs76 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #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: 294
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 02-08-2008, 11:25 PM
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT +3. The time now is 10:50 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org