账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    非常诡异的内存溢出,希望有大佬教育一波
    33
    0

    项目框架 spring boot 2.0
    我写了 父类 BaseController 里面自动注入 request 和 response


    public  class BaseController{
    
        @Autowired
        public HttpServletRequest request;
    
        @Autowired
        public HttpServletResponse response;
    
    }
    

    IndexController 都继承 BaseController


    public class IndexController extends BaseController {

    @RequestMapping("/index")
    public String index(Model model) {
        return "index";
    }
    
    @RequestMapping("login")
    public String login() throws ServletException, IOException {
        request.getRequestDispatcher("/admin/index").forward(request, response);
        return null;
    }
    

    index 页面,没有任何数据操作,纯一个静态页面
    <!DOCTYPE html>
    <html>

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="renderer" content="webkit">
    <title>500</title>

    </head>

    <body style="background: #FFFFFF; color: red; font-size: 40em; text-align: center">

    <p>
        TEST
    </p>

    </body>
    </html>


    访问 http://localhost:9090/admin/login
    出现如下错误,循环引用导致内存溢出
    java.lang.StackOverflowError: null

    at org.springframework.web.context.support.WebApplicationContextUtils.currentRequestAttributes(WebApplicationContextUtils.java:312)
    at org.springframework.web.context.support.WebApplicationContextUtils.access$400(WebApplicationContextUtils.java:65)
    at org.springframework.web.context.support.WebApplicationContextUtils$RequestObjectFactory.getObject(WebApplicationContextUtils.java:328)
    at org.springframework.web.context.support.WebApplicationContextUtils$RequestObjectFactory.getObject(WebApplicationContextUtils.java:323)
    at org.springframework.beans.factory.support.AutowireUtils$ObjectFactoryDelegatingInvocationHandler.invoke(AutowireUtils.java:305)
    at com.sun.proxy.$Proxy100.setAttribute(Unknown Source)
    at org.apache.catalina.core.ApplicationHttpRequest.setAttribute(ApplicationHttpRequest.java:304)
    at sun.reflect.GeneratedMethodAccessor95.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.beans.factory.support.AutowireUtils$ObjectFactoryDelegatingInvocationHandler.invoke(AutowireUtils.java:305)
    at com.sun.proxy.$Proxy100.setAttribute(Unknown Source)
    at org.apache.catalina.core.ApplicationHttpRequest.setAttribute(ApplicationHttpRequest.java:304)
    at sun.reflect.GeneratedMethodAccessor95.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.beans.factory.support.AutowireUtils$ObjectFactoryDelegatingInvocationHandler.invoke(AutowireUtils.java:305)
    at com.sun.proxy.$Proxy100.setAttribute(Unknown Source)
    at org.apache.catalina.core.ApplicationHttpRequest.setAttribute(ApplicationHttpRequest.java:304)
    at sun.reflect.GeneratedMethodAccessor95.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.beans.factory.support.AutowireUtils$ObjectFactoryDelegatingInvocationHandler.invoke(AutowireUtils.java:305)
    at com.sun.proxy.$Proxy100.setAttribute(Unknown Source)
    at org.apache.catalina.core.ApplicationHttpRequest.setAttribute(ApplicationHttpRequest.java:304)
    at sun.reflect.GeneratedMethodAccessor95.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)

    1.注意看我的login 就使用request转发到首页(注意,此时request 和 response 是父类的BaseController)

    1. 注意问题不是地址循环跳引发的问题

    3.使用方法注入request 和 response就不会有问题
    @RequestMapping("login")

    public String login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/admin/index").forward(request, response);
        return null;
    }
    

    有没有大佬解释一波。

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 画船听雨眠~ 普通会员 1楼

      内存溢出通常指的是程序运行时内存的分配超过了预期的范围,导致系统无法提供足够的内存来保存正在处理的数据。内存溢出的情况在很多场景中都会发生,例如:

      1. 当程序需要使用大量数据进行处理时,但系统内存无法满足需求,就会出现内存溢出。

      2. 当程序运行时,如果系统缓存中没有足够的数据供程序使用,也可能导致内存溢出。

      3. 当程序需要使用大量的CPU资源,如执行大量的I/O操作,也可能会导致内存溢出。

      要解决内存溢出的问题,可以采取以下几种策略:

      1. 对程序进行内存管理,避免程序无限制地使用内存。例如,可以设置程序的最大内存使用量,当程序使用到这个量时,程序就会自动退出。

      2. 对于需要大量数据进行处理的程序,可以考虑使用多线程或多进程来分摊内存的使用。这样可以有效地利用多核CPU,避免内存溢出。

      3. 对于需要大量CPU资源的程序,可以考虑使用多核CPU,或者使用专门的CPU管理工具来优化CPU的使用。

      4. 对于需要大量I/O操作的程序,可以考虑使用多线程或多进程来分摊I/O的使用。这样可以有效地利用多核CPU,避免I/O操作带来的内存溢出。

      以上只是解决内存溢出的一些基本策略,具体的解决策略需要根据程序的特性和需求来确定。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部