Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

java.lang.OuofMemory Error

Posted on 2003-03-24
14
Medium Priority
?
317 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
  • 2
  • 2
  • 2
  • +7
13 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues undeā€¦
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month11 days, 10 hours left to enroll

564 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