1

I want to extract nouns from selected text on my webpage, and highlight them when the text is displayed.So I use OpenNLP library for parsing and getting the list of nouns. It runs well in java class without memory problem though it took 6-7 seconds before showing the output, but when I run the code in jsp page, I got these error:

    javax.servlet.ServletException: java.lang.OutOfMemoryError: Java heap space
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:909)
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:838)
    org.apache.jsp.highlightText_jsp._jspService(highlightText_jsp.java:294)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

root cause

java.lang.OutOfMemoryError: Java heap space
    opennlp.model.AbstractModelReader.getParameters(AbstractModelReader.java:144)
    opennlp.maxent.io.GISModelReader.constructModel(GISModelReader.java:75)
    opennlp.model.GenericModelReader.constructModel(GenericModelReader.java:59)
    opennlp.model.AbstractModelReader.getModel(AbstractModelReader.java:87)
    opennlp.tools.util.model.GenericModelSerializer.create(GenericModelSerializer.java:35)
    opennlp.tools.util.model.GenericModelSerializer.create(GenericModelSerializer.java:31)
    opennlp.tools.util.model.BaseModel.loadModel(BaseModel.java:231)
    opennlp.tools.util.model.BaseModel.<init>(BaseModel.java:181)
    opennlp.tools.postag.POSModel.<init>(POSModel.java:82)
    opennlp.tools.parser.ParserModel$POSModelSerializer.create(ParserModel.java:49)
    opennlp.tools.parser.ParserModel$POSModelSerializer.create(ParserModel.java:45)
    opennlp.tools.util.model.BaseModel.finishLoadingArtifacts(BaseModel.java:303)
    opennlp.tools.util.model.BaseModel.loadModel(BaseModel.java:240)
    opennlp.tools.util.model.BaseModel.<init>(BaseModel.java:181)
    opennlp.tools.parser.ParserModel.<init>(ParserModel.java:152)
    model.parser.parserAction(parser.java:59)
    org.apache.jsp.highlightText_jsp._jspService(highlightText_jsp.java:143)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

Some solutions I read suggest to increase heap memory size of apache tomcat by doing this:

set CATALINA_OPTS=-Xms256m -Xmx512m

so I set it to -Xmx2g, but it gives the same error still. I dont think I need to modify JVM heap size,(Right click project in Netbeans > Properties > Run > VM Options > set -Xmx) but did try it though and it wasn't successfull.

Never had this error before, so I have no idea what should I do. Appreciate any help, thanks.

user3749677
  • 11
  • 1
  • 2
  • Did you try profiling your code and see what objects are getting created and which ones are using maximum memory, chances of memory leaks, etc? Your Oracle JDK comes with VisualVM which can get you started.. – anacron Mar 13 '17 at 08:28
  • I guess this way yo are increasing NetBeans heap space, but not Tomcat heap space. Try to use jconsole or visualvm to connect to tomcat and see how many memory has the heap. – malaguna Mar 13 '17 at 08:29
  • Does this answer your question? [java.lang.OutOfMemoryError: Java heap space with NetBeans](https://stackoverflow.com/questions/717550/java-lang-outofmemoryerror-java-heap-space-with-netbeans) – Rahul Jain Feb 27 '20 at 07:27

3 Answers3

0

For such problems you can (and should) use a tool like VisualVM. It is bundled with the JDK under jdk1.8.0_xxx\bin\jvisualvm.exe and you can connect to the local JVMs without any configuration. Using the tool you should be able to find out where and why your application runs out of memory.

Daniel Szalay
  • 4,041
  • 12
  • 57
  • 103
0

The java.lang.OutOfMemoryError: Java heap space error will be triggered when the application attempts to add more data into the heap space area, but there is not enough room for it. An easy way to solve OutOfMemoryError in java is to increase the maximum heap size by using JVM options "-Xmx512M", which you already have tried.

The second way to resolve OutOfMemoryError in Java is You can use Eclipse Memory Analyzer to examine your heap dump or you can use any profiler like Netbeans or JProbe.

In your case, as you are trying extract nouns from webpage and highlighting them as well. So just try to find out whether in your code is there any loop which turns out to be a infinite loop in certain condition.

arpit garg
  • 61
  • 5
  • If the application is leaking memory, as is probable, increasing the heap size will only defer the problem, not solve it. – user207421 Mar 13 '17 at 08:47
0

Since you mentioned that you still have problem even if you increase the heap memory to 2gb, it is clear that you have memory leak in your code. Unused objects are not dereferenced so that they would be removed by the Garbage collector.

Using more heap memory than actually required would have performance impact. Over provisioning of heap memory does not solve the issue in your case. Instead it would have poor performance (lengthy pause time). You need to profile your code using any profiler (Java Mission Control, Java Visual VM, Your Kit java profiler, etc.) to see which object takes more memory space in heap. If you can share your code, then we will be able to find where exactly the memory leak occurs.

Nirmal
  • 89
  • 6