0

I have made some modification in a code of an existing application. While testing i am getting Exception created : java.lang.OutOfMemoryError. But the error occurs only once in a while. Below the is the code snippet where the error occurs

}else if(subject.equals("Mobile")){
 to=(String)hashMap.get("M_MOBILETOMAIL");
 m_mobileoptionvalue=(String)parameters.get("m_mobileoptionvalue");
 m_mobileq1value=(String)parameters.get("m_mobileq1value");
 StringTokenizer m_tokenizer1 = new StringTokenizer(m_mobileq1value,"|");
 while (m_tokenizer1.hasMoreTokens()){
 m_mobileq1List.add(m_tokenizer1.nextToken());
 }
 m_mobileq2value=(String)parameters.get("m_mobileq2value");
 StringTokenizer m_tokenizer2 = new StringTokenizer(m_mobileq2value,"|");
 while (m_tokenizer2.hasMoreTokens()){
 m_mobileq2List.add(m_mobileq2value);
 }
 m_mobileq3value=(String)parameters.get("m_mobileq3value");
 StringTokenizer m_tokenizer3 = new StringTokenizer(m_mobileq3value,"|");
 while (m_tokenizer3.hasMoreTokens()){
 m_mobileq3List.add(m_mobileq3value);
 }
 m_mobileq4value=(String)parameters.get("m_mobileq4value");
 m_mobileq4=(String)parameters.get("m_mobileq4");
 }

The error i am gettting is in the line

m_mobileq2List.add(m_mobileq2value);

Also attaching the JVM logs ----

exception created in one of the service methods of the servlet MailSend in application interact_assorted_intapp7. Exception created : java.lang.OutOfMemoryError
 at java.util.ArrayList.newElementArray(ArrayList.java:94)
 at java.util.ArrayList.growAtEnd(ArrayList.java:375)
 at java.util.ArrayList.add(ArrayList.java:158)
 at com.international.servlets.MailSend.doPost(MailSend.java:473)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:738)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:831)

I went through a few related post but did not get any proper results.Also Increase of HeapSize is out of scope.

Andrew Thompson
169k41 gold badges224 silver badges439 bronze badges
asked Jan 16, 2014 at 11:09
5
  • 3
    Could you be out of memory? Commented Jan 16, 2014 at 11:11
  • I think, the reason behind this is memory leakage . Make sure all your variables have weak or phantom reference after you are done with them and you must be careful not to induce a strong reference to any dead object. Commented Jan 16, 2014 at 11:12
  • You need the give the JVM more memory to work with or you might have a memory leak somewhere. Commented Jan 16, 2014 at 11:13
  • @TheDarkKnight Thanks a lot for the input. Can you please be a bit elaborate. I am not a pro java resource so if you could please elaborate a bit, it will be helpfull. Commented Jan 16, 2014 at 11:14
  • Look at Matt's answer below. If this is the case, then every iteration that you are doing is adding a lot of same instance of the mobileq2value objects in your heap and they are not getting garbage collected.They become strong references of tokenizer2 and JVM being a moody uncle may decide not to garbage collect them , even when they are out of scope. Commented Jan 16, 2014 at 11:18

3 Answers 3

2
while (m_tokenizer2.hasMoreTokens()){
 m_mobileq2List.add(m_mobileq2value);
}

You are never moving your tokenizer pointer forward, so when this condition is met, it is infinitely adding the first token to your list. Try

while (m_tokenizer2.hasMoreTokens()){
 m_mobileq2List.add(m_tokenizer2.nextToken());
 }
answered Jan 16, 2014 at 11:15
Sign up to request clarification or add additional context in comments.

2 Comments

Note that from the StringTokenizer API page: "StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead"
Thanks a lot Matt for the resolution.
0

If you are running out of memory and you can't increase the heap size then all you can do is try and use less memory.

Attach a profiler of some kind to your application (most IDEs have one built in) and look at where the memory is going and what you can do to reduce it, or remove any potential resource leaks you may have.

It's also worth running findbugs against your project and seeing if that finds anything. Again it's available as a plugin for most IDEs.

answered Jan 16, 2014 at 11:13

Comments

0

Issue in your code is infinite while loop.change your code to

 m_mobileq2List.add(m_tokenizer2.nextToken());

Also make null all your Strings after use.Go for StringBuffer,StringBuilder instead of Strings whenever possible.If you are using any Input/Output Stream close them after use and make them null.Making large objects null saves lot of memory.

answered Jan 16, 2014 at 11:24

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.