Java 根本原因 java.lang.OutOfMemoryError 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4959172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 08:47:47  来源:igfitidea点击:

Java root cause java.lang.OutOfMemoryError error

javaout-of-memory

提问by Ravikumar

I am new to Java and given the task to fix a bug and the issue is as follows. It would be really great if you give suggestions/ideas what is this issue and how can I fix this.:

我是 Java 新手,被赋予修复错误的任务,问题如下。如果您提出建议/想法这是什么问题以及我该如何解决这个问题,那就太好了。:

HTTP Status 500 - 

--------------------------------------------------------------------------------

type Exception report

message 

description The server encountered an internal error () that prevented it from fulfilling this request.

exception 

org.apache.jasper.JasperException
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:453)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:375)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:368)


root cause 

javax.servlet.ServletException
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:858)
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:791)
    org.apache.jsp.CustMaint.Jsp.ProfProfileDetails_jsp._jspService(ProfProfileDetails_jsp.java:4016)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:368)


root cause 

java.lang.OutOfMemoryError


note The full stack trace of the root cause is available in the Apache Tomcat/5.5.17 logs.


--------------------------------------------------------------------------------

Apache Tomcat/5.5.17

回答by Sean Patrick Floyd

Here's what the Tomcat guys have to say:

以下是 Tomcat 的家伙们不得不说的:

An Out Of Memory can be thrown by several causes:

  • A servlet trying to load a several GBytes file into memory will surely kill the server. These kind of errors must be considered a simple bug in our program.

  • To compensate for the data your servlet tries to load, you increase the heap size so that there is no room to create the stack size for the threads that need to be created. The memory required by each thread will vary by OS but can be as high as 2M by default and in some OS's (like Debian Sarge) is not reducible with the -Xssparameter. 1 Rule of Thumb, use no more than 1G for heap space in a 32-bit web application.

  • Deep recursive algorithms can also lead to Out Of Memory problems. In this case, the only fixes are increasing the thread stack size (-Xss), or refactoring the algorithms to reduce the depth, or the local data size per call.

  • A webapp that uses lots of libraries with many dependencies, or a server maintaining lots of webapps could exhauste the JVM PermGen space. This space is where the VM stores the classes and methods data. In those cases, the fix is to increase this size. The Sun VM has the flag -XX:MaxPermSizethat allows to set its size (the default value is 64M)

  • Hard references to classes can prevent the garbage collector from reclaiming the memory allocated for them when a ClassLoaderis discarded. This will occur on JSP recompilations, and webapps reloads. If these operations are common in a webapp having these kinds of problems, it will be a matter of time, until the PermGen space gets full and an Out Of Memory is thrown.

内存不足可能由多种原因引发:

  • 尝试将几个 GBytes 文件加载到内存中的 servlet 肯定会杀死服务器。这些类型的错误必须被视为我们程序中的一个简单错误。

  • 为了补偿您的 servlet 尝试加载的数据,您增加了堆大小,以便没有空间为需要创建的线程创建堆栈大小。每个线程所需的内存因操作系统而异,但默认情况下可高达 2M,并且在某些操作系统(如 Debian Sarge)中无法使用该-Xss参数减少。1 经验法则,在 32 位 Web 应用程序中使用不超过 1G 的堆空间。

  • 深度递归算法也可能导致内存不足问题。在这种情况下,唯一的修复是增加线程堆栈大小 ( -Xss),或重构算法以减少深度,或每次调用的本地数据大小。

  • 使用大量依赖项的大量库的 Web 应用程序,或者维护大量 Web 应用程序的服务器可能会耗尽 JVM PermGen 空间。这个空间是 VM 存储类和方法数据的地方。在这些情况下,解决方法是增加此大小。Sun VM 具有-XX:MaxPermSize允许设置其大小的标志 (默认值为 64M)

  • 对类的硬引用可以防止垃圾收集器在 aClassLoader被丢弃时回收为它们分配的内存 。这将在 JSP 重新编译和 webapps 重新加载时发生。如果这些操作在具有此类问题的 web 应用程序中很常见,那将是时间问题,直到 PermGen 空间变满并抛出 Out Of Memory。

Source:Tomcat Wiki: OutOfMemory

来源:Tomcat Wiki:OutOfMemory

回答by irreputable

Well... who reallycaused the out of memory error?

嗯......谁真正导致了内存不足错误?

If you ate 8 slices of pizza and you are full, is it the last slice that caused the out of stomach error?

如果你吃了 8 片比萨而且你已经吃饱了,是不是最后一片导致了胃外错误?

回答by BalusC

This kind of problem is not easy to nail down based on just the stacktrace. It at least boils down to that you've either a memory leak in your application (the code is keeping (unnecessarily) too much objects for an (unnecessarily) long time in memory), or the server simply doesn't have enough memory in order to be able to run your webapp (simply because it is designed that way to require many memory).

仅凭堆栈跟踪就不容易确定此类问题。至少可以归结为您的应用程序中存在内存泄漏(代码在内存中(不必要地)长时间保留(不必要地)太多对象),或者服务器根本没有足够的内存为了能够运行您的 web 应用程序(仅仅是因为它的设计方式需要很多内存)。

To detect and fix memory leaks, use a Java profiler. If you don't have any memory leaks, i.e. the memory usage is stable but the code just really need that much memory, then simply give the server more memory to work with. The profiler is however still useful to spot memory holes in your webapp and optimize code accordingly.

要检测和修复内存泄漏,请使用 Java 分析器。如果您没有任何内存泄漏,即内存使用情况稳定但代码确实需要那么多内存,那么只需为服务器提供更多内存即可使用。然而,分析器对于发现 web 应用程序中的内存漏洞并相应地优化代码仍然很有用。

If you're using Eclipse, use TPTP profileror if you're using Netbeans, use builtin VisualVM profiler. Or when you're using standalone VisualVM, check this bloghow to monitor Tomcat with it.

如果您使用 Eclipse,请使用TPTP 分析器,或者如果您使用 Netbeans,请使用内置的VisualVM 分析器。或者,当您使用独立的 VisualVM 时,请查看此博客如何使用它监控 Tomcat。

回答by Nishant

Use Java Heap Analysis Tool (JHAT) with Eclipse MAT http://www.eclipse.org/mat/to analyse what's going on inside JVM. What is eating how much memory. See the profile and then see the code causing that.

使用 Java 堆分析工具 ( JHAT) 和 Eclipse MAT http://www.eclipse.org/mat/分析 JVM 内部发生的事情。什么吃多少内存。查看配置文件,然后查看导致该问题的代码。

You can also use JConsole, it's dead easy to set it up. And you can see stuffs 'live'. TPTP is also a good option, unfortunately, I find it hard to configure.

您也可以使用JConsole,设置它非常容易。你可以看到“活”的东西。TPTP 也是一个不错的选择,不幸的是,我发现它很难配置。