user1643087
user1643087

Reputation: 643

Exception created : java.lang.OutOfMemoryError

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.

Upvotes: 0

Views: 975

Answers (3)

Swapnil
Swapnil

Reputation: 2502

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.

Upvotes: 0

Matt
Matt

Reputation: 3353

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());
 }

Upvotes: 2

Tim B
Tim B

Reputation: 41188

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.

Upvotes: 0

Related Questions