?
Solved

java.lang.OuofMemory Error

Posted on 2003-03-24
14
Medium Priority
?
313 Views
Last Modified: 2012-06-27
We are getting java.lang.OuofMemory Error while adding/modifying the form field values to database. The values are in bulk quantity so we are storing them by string concatenation.
0
Comment
Question by:fakhry
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +7
14 Comments
 
LVL 3

Accepted Solution

by:
allahabad earned 2000 total points
ID: 8200603
1. Use StringBuffer and append method of this class to build the final string. Since String is immutable object, String contcatenation creates temporary objects .
2. Increase the max. heap size .
0
 
LVL 2

Expert Comment

by:k_suchdeva
ID: 8200609
Run the program again with following command line options

java -Xms -Xmx <your main class>

Where

    -Xms<size>        set initial Java heap size
    -Xmx<size>        set maximum Java heap size

You can start with an initial value of 64M for initial size and 128M for maximum size.
java -Xms64mb -Xmx128mb <your main class>
and then keep increasing it untill you stop gettiing OutofMemoryError.

Also try tweaking your application so that you do not get these OutofMemoryError's at first place. There can be some memory leaks in your program. So try to fix them using some optimizer.

Regards
Khem
0
 
LVL 3

Expert Comment

by:allahabad
ID: 8200643
Also release the object when it is not needed by assigning null to its reference, and let garbage collector release the memory for this object, when it runs. You can also request garbage collector by doing System.gc(), but GC may not run immidiately. Its low priorty thread, it runs when memory is low, or when no high priorty threads are running,  or also it depends on the algorthim impelmented in JVM to call GC.    
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 9

Expert Comment

by:Venci75
ID: 8201054
Is it really needed to use Strings? Can't you directly use bytes? I mean - something like that:
ByteArrayOutputStream out = new ByteArrayOutputStream(<appropriate size>);
out.write(byte[]);
out.write(byte[]);
...
byte[] buffer = out.toByteArray();

... or even better - stream the data to directly to the database?

What database are you using? Can you post some code showing how do you process the data?
0
 
LVL 35

Expert Comment

by:girionis
ID: 8201368
 YOu can also get a tool like OptimizeIt (http://www.borland.com/optimizeit/) and see where the potential leaks occur.
0
 

Expert Comment

by:kartaltabak
ID: 8202740
This is not an answer, just comment on two other comments.

to: allahabad...

temporary objects only effect performance, quickly fills the memory; but donot cause ajava.lang.OutOfMemoryError.

Before the JVM causes an OutOfMemoryError, it allows a garbage collection which will cause (almost) all unreferenced objects to be freed.

to: venci75...

this method will fail on non-Latin1 character sets. You should use char[] instead.

Otherwise, you have to use the character conversion by yourself, whereas if you use Strings, java does it automatically.

0
 
LVL 9

Expert Comment

by:Venci75
ID: 8202770
kartaltabak >> have you read my comment? Have I  mentioned something about chars/charsets and convertions?
0
 
LVL 1

Expert Comment

by:LC/OS
ID: 8220764
Sometimes, when the program is working flat out neither garbage collection nor dynamic heap allocation react fast enough. In this circumstance you will run out of heap because you using it faster than it can be allocated ! Yes, it does happen - I've seen it !!

You can start with a large ammount of heap, call gc() your self, remove some of the heavy loading etc., etc..
0
 

Expert Comment

by:keithfry
ID: 8222733
* Use facilities to stream to a CLOB. (Same as Venci75's comment)
  AND
* Tighten your commit() scope if you are writing many rows as your DB driver may be holding references to your Strings or char data until they are committed.

  OR

* Write a stored procedure that is used to insert the rows. This may push the memory requirements to the database.

  OR

* Increase your JVM memory profile and buy more RAM as necessary.  :-)
0
 
LVL 2

Expert Comment

by:k_suchdeva
ID: 8222755
fakhry has not posted even a single comment and we are getting mad discussing all the possible options.

This is really annoying because every time someone updates the question, a mail is fired to everyone in the list. So we should refrain from putting so many comments unless the submitter says something.

Just a suggestion !!

Khem
0
 

Author Comment

by:fakhry
ID: 8655421
Use StringBuffer and append method of this class to build the final string. Since String is immutable object, String contcatenation creates temporary objects . thanks this helped me a lot i did it and it worked
0
 

Expert Comment

by:CleanupPing
ID: 9058506
fakhry:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9732121
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept allahabad's comment as answer.

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jimmack
EE Cleanup Volunteer
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month12 days, 22 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question