- 浏览: 7874967 次
- 性别:
- 来自: 广州
文章分类
- 全部博客 (2425)
- 软件工程 (75)
- JAVA相关 (662)
- ajax/web相关 (351)
- 数据库相关/oracle (218)
- PHP (147)
- UNIX/LINUX/FREEBSD/solaris (118)
- 音乐探讨 (1)
- 闲话 (11)
- 网络安全等 (21)
- .NET (153)
- ROR和GOG (10)
- [网站分类]4.其他技术区 (181)
- 算法等 (7)
- [随笔分类]SOA (8)
- 收藏区 (71)
- 金融证券 (4)
- [网站分类]5.企业信息化 (3)
- c&c++学习 (1)
- 读书区 (11)
- 其它 (10)
- 收藏夹 (1)
- 设计模式 (1)
- FLEX (14)
- Android (98)
- 软件工程心理学系列 (4)
- HTML5 (6)
- C/C++ (0)
- 数据结构 (0)
- 书评 (3)
- python (17)
- NOSQL (10)
- MYSQL (85)
- java之各类测试 (18)
- nodejs (1)
- JAVA (1)
- neo4j (3)
- VUE (4)
- docker相关 (1)
最新评论
-
xiaobadi:
jacky~~~~~~~~~
推荐两个不错的mybatis GUI生成工具 -
masuweng:
(转)JAVA获得机器码的实现 -
albert0707:
有些扩展名为null
java 7中可以判断文件的contenttype了 -
albert0707:
非常感谢!!!!!!!!!
java 7中可以判断文件的contenttype了 -
zhangle:
https://zhuban.me竹板共享 - 高效便捷的文档 ...
一个不错的网络白板工具
在老外http://vard-lokkur.blogspot.com/2012/12/springs-web-mvc-redirect-to-memory-leak.html一文中,谈到了他发现的一个spring mvc 3以及之前版本可能存在的一个
redirect引起的内存泄露问题。例子为:
You may start from reading Resolving views in Spring Framework documentation, and then take a closer look at the source code of AbstractCachingViewResolver, which is base class for many different View Resolvers in Spring, including: JSP, FreeMarker, Velocity, Jasper Reports, Tiles and XSLT view resolvers.
When resolveViewName method is called on AbstractCachingViewResolver it uses HashMap based view cache to speed up view resolving in the future calls, and cache key is by default created using view name and current locale.
Now to the clue - when you use the above method of redirecting, Spring Framework uses the whole String returned from your controller's method as the view name, including all parameters included in the target URL. Each time you perform the redirect, the parameters may vary, thus such a redirect will leave one additional entry in view cache of AbstractCachingViewResolver, causing memory leak.
他认为,只要这个形式,多次的执行的话(他执行了7000多次),看JVM大小设置问题会导致内存泄露,原因他分析说,AbstractCachingViewResolver,这个代码是众多视图解析的父类了,
当 resolveViewName 方法调用AbstractCachingViewResolver时,使用的是hashmap
缓存去加速,而key是默认使用viewname和当前的locale.而spring当调用
redirect这个时候,使用的是整个从controller方法中返回的字符串,做为view name,包括你的目标url,所以导致了memory leak(但为什么呢?好像原文作者没具体分析出来)
作者还在https://jira.springsource.org/browse/SPR-10065开了一个jira说明,
有兴趣的读者可以去看下。他测试的DEMO的例子在:
https://github.com/vardlokkur/webapp-02可以下载。
不过可惜我在自己的机器上,chrome下执行7000多次,没发现有问题,但IE下执行到1040
次,没报错,但就停止了,很奇怪,大家可以探讨尝试下。
redirect引起的内存泄露问题。例子为:
@RequestMapping(method = RequestMethod.POST) public String onPost(...) { ... return "redirect:form.html?entityId=" + entityId; }
You may start from reading Resolving views in Spring Framework documentation, and then take a closer look at the source code of AbstractCachingViewResolver, which is base class for many different View Resolvers in Spring, including: JSP, FreeMarker, Velocity, Jasper Reports, Tiles and XSLT view resolvers.
When resolveViewName method is called on AbstractCachingViewResolver it uses HashMap based view cache to speed up view resolving in the future calls, and cache key is by default created using view name and current locale.
Now to the clue - when you use the above method of redirecting, Spring Framework uses the whole String returned from your controller's method as the view name, including all parameters included in the target URL. Each time you perform the redirect, the parameters may vary, thus such a redirect will leave one additional entry in view cache of AbstractCachingViewResolver, causing memory leak.
他认为,只要这个形式,多次的执行的话(他执行了7000多次),看JVM大小设置问题会导致内存泄露,原因他分析说,AbstractCachingViewResolver,这个代码是众多视图解析的父类了,
当 resolveViewName 方法调用AbstractCachingViewResolver时,使用的是hashmap
缓存去加速,而key是默认使用viewname和当前的locale.而spring当调用
redirect这个时候,使用的是整个从controller方法中返回的字符串,做为view name,包括你的目标url,所以导致了memory leak(但为什么呢?好像原文作者没具体分析出来)
作者还在https://jira.springsource.org/browse/SPR-10065开了一个jira说明,
有兴趣的读者可以去看下。他测试的DEMO的例子在:
https://github.com/vardlokkur/webapp-02可以下载。
不过可惜我在自己的机器上,chrome下执行7000多次,没发现有问题,但IE下执行到1040
次,没报错,但就停止了,很奇怪,大家可以探讨尝试下。
评论
3 楼
tomgxf
2016-11-24
感谢楼主提供的线索,旧版本的确有问题。
在框架升级的时候我们发现该问题在新版本的springmvc中已经得到解决。缓存的map添加了默认数量限制。
在框架升级的时候我们发现该问题在新版本的springmvc中已经得到解决。缓存的map添加了默认数量限制。
private final Map<Object, View> viewCreationCache = new LinkedHashMap<Object, View>(DEFAULT_CACHE_LIMIT, 0.75f, true) { @Override protected boolean removeEldestEntry(Map.Entry<Object, View> eldest) { if (size() > getCacheLimit()) { viewAccessCache.remove(eldest.getKey()); return true; } else { return false; } } };
2 楼
jackyrong
2012-12-09
是的,在老外的这个jira中,后来也说到了addAttribute的方法了,呵呵
1 楼
zfc827
2012-12-09
这个问题确实存在,主要原因是因为 springmvc 会缓存 view 的key。关键代码如下:
以上代码定义了一个 cache的标识位,默认为 true,并定义了一个 Map 来缓存 View,
如果在 controller 返回的 view 是不固定的,如:"redirect:form.html?entityId=" + entityId,由于 entityId 的值会存在 N 个,那么会导致产生 N 个 ViewName 被缓存起来。
由于内存大小是有限的,所以当 N 大到一定数量时,会产生内存溢出。
个人认为,这并不能算是 spring 的bug,更多的应当是我们的使用方式导致。
为了解决这个问题,spring 提供了其他的方案:http://static.springsource.org/spring-framework/docs/3.2.0.BUILD-SNAPSHOT/reference/htmlsingle/#mvc-ann-redirect-attributes
方法修改如下:
如上,使用占位符后, viewName 的数量被限制为了1个。
public abstract class AbstractCachingViewResolver extends WebApplicationObjectSupport implements ViewResolver { /** Whether we should cache views, once resolved */ private boolean cache = true; /** Map from view key to View instance */ private final Map<Object, View> viewCache = new HashMap<Object, View>(); //... set/get方法略 public View resolveViewName(String viewName, Locale locale) throws Exception { if (!isCache()) { return createView(viewName, locale); } else { Object cacheKey = getCacheKey(viewName, locale); synchronized (this.viewCache) { View view = this.viewCache.get(cacheKey); if (view == null && (!this.cacheUnresolved || !this.viewCache.containsKey(cacheKey))) { // Ask the subclass to create the View object. view = createView(viewName, locale); if (view != null || this.cacheUnresolved) { this.viewCache.put(cacheKey, view); if (logger.isTraceEnabled()) { logger.trace("Cached view [" + cacheKey + "]"); } } } return view; } } } }
以上代码定义了一个 cache的标识位,默认为 true,并定义了一个 Map 来缓存 View,
如果在 controller 返回的 view 是不固定的,如:"redirect:form.html?entityId=" + entityId,由于 entityId 的值会存在 N 个,那么会导致产生 N 个 ViewName 被缓存起来。
由于内存大小是有限的,所以当 N 大到一定数量时,会产生内存溢出。
个人认为,这并不能算是 spring 的bug,更多的应当是我们的使用方式导致。
为了解决这个问题,spring 提供了其他的方案:http://static.springsource.org/spring-framework/docs/3.2.0.BUILD-SNAPSHOT/reference/htmlsingle/#mvc-ann-redirect-attributes
方法修改如下:
@RequestMapping(method = RequestMethod.POST) public String onPost(RedirectAttributes redirectAttrs) { // ... redirectAttrs.addAttribute("entityId", entityId) return "redirect:form.html?entityId={entityId}"; }
如上,使用占位符后, viewName 的数量被限制为了1个。
发表评论
-
复习:强迫线程顺序执行方式
2019-01-03 23:42 1506方法1: 三个线程,t1,t2,t3,如果一定要按顺序执行, ... -
(转)不错的前后端处理异常的方法
2019-01-02 23:16 1981前言 在 Web 开发中, 我们经常会需要处理各种异常, 这是 ... -
info q的极客时间大咖说等资料下载
2018-08-15 08:40 3427info q的极客时间大咖说等资料下载,还有不少思维导图 链 ... -
CXF 客户端超时时间设置(非Spring配置方式)
2018-07-03 22:38 2199import org.apache.cxf.endpoint. ... -
(转)synchronized关键字画像:正确打开方式
2018-06-14 09:25 459https://mp.weixin.qq.com/s/b3Sx ... -
CountDownLatch的例子
2018-06-13 14:10 646public class StatsDemo { ... -
两道面试题,带你解析Java类加载机制
2018-06-12 16:29 568https://mp.weixin.qq.com/s/YTa0 ... -
Spring中获取request的几种方法,及其线程安全性分析
2018-06-11 09:03 634https://mp.weixin.qq.com/s/KeFJ ... -
内部类小结
2018-06-06 10:25 409https://mp.weixin.qq.com/s/hErv ... -
JVM虚拟机小结1
2018-06-04 20:43 4731 jps -l //列出详细的类名和进程ID 2)jps ... -
windows下自带命令行工具查看CPU资源情况等
2018-06-04 12:53 3056微软提供了不少命令行 ... -
(收藏)深入分析Java的序列化与反序列化
2018-05-30 15:21 575https://mp.weixin.qq.com/s/T2Bn ... -
apache common包中的序列化工具
2018-05-30 09:10 1794什么是序列化 我们的 ... -
JAVA8 JVM的变化: 元空间(Metaspace)
2018-05-24 22:30 927本文将会分享至今为至我收集的关于永久代(Permanent G ... -
(转)服务器性能指标(一)——负载(Load)分析及问题排查
2018-05-21 21:03 1290原创: Hollis Hollis 负载 ... -
(转)对象复用
2018-05-20 15:27 823public class Student { priv ... -
mapreduce中入门中要注意的几点
2018-05-06 08:59 635在 mapreduce中,比如有如下的词: I love b ... -
HDFS的基本操作
2018-05-02 21:47 897-mkdir 在HDFS创建目录 ... -
一个不错的开源工具类,专门用来解析日志头部的,好用
2018-05-02 20:00 724一个不错的开源工具类,专门用来解析日志头部的,好用。 http ... -
介绍个不错的RESTFUL MOCK的工具wiremock
2018-04-27 21:02 1869介绍个不错的RESTFUL MOCK的工具wiremock,地 ...
相关推荐
案例8:Spring整合Spring MVC与Hibernate + EasyUI实现电子商城后台订单管理.docx案例8:Spring整合Spring MVC与Hibernate + EasyUI实现电子商城后台订单管理.docx案例8:Spring整合Spring MVC与Hibernate + EasyUI...
案例8:Spring整合Spring MVC与Hibernate + EasyUI实现电子商城后台订单管理.pdf案例8:Spring整合Spring MVC与Hibernate + EasyUI实现电子商城后台订单管理.pdf案例8:Spring整合Spring MVC与Hibernate + EasyUI...
1.创建第一个 Spring MVC 程序案例 2.Spring MVC @RequestMapping 注解案例 3.Spring MVC 请求参数的获取案例 4.Spring MVC 域对象共享数据案例 5.Spring MVC @ModelAttribute 注解案例 6.Spring MVC 国际化案例 7....
Spring MVC 到底是如何工作的?1
spring mvc重定向导致内存溢出解决方案
该毕业设计模仿了微博的功能,使用技术:spring mvc,spring,mybatis,websocket,页面较为清爽。 系统功能包括:分享新鲜事,点赞,收藏,回复等。因为使用了websocket,所以当别人点赞或者回复的时候,服务器端会...
_第一课:spring mvc 原理深度解析1
赠送jar包:spring-webmvc-5.0.8.RELEASE.jar; 赠送原API文档:spring-webmvc-5.0.8.RELEASE-javadoc.jar; 赠送源代码:spring-webmvc-5.0.8.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-webmvc-5.0.8....
开源测试项目:spring mvc+springsecurity3+ehcache+bootstrap+mysql 内附MySQL表,直接导入就可运行 效果图请移步:http://blog.csdn.net/yangxuan0261/article/details/10053947
NULL 博文链接:https://shmilyaw-hotmail-com.iteye.com/blog/2246344
互联网轻量级SSM框架解密:Spring、Spring MVC、MyBatis源码深度剖析 非影印版,高清,带目录书签!!!
, 在精通Spring MVC4中,我们将会从头开始构建一个有用的Web应用。本书共计10章,分别介绍了快速搭建Spring Web应用、精通MVC结构、URL映射、文件上传与错误处理、创建Restful应用、保护应用、单元测试与验收测试、...
Web 框架,即使用了 MVC(模型-视图-控制器)架构模式的思想,将 web 层进行职责解耦,基于请求驱动指的就是使用请求-响应模型,框架的目的就是帮助我们简化开发,Spring MVC 也是一样,它只关注于三层中的控制层,...
赠送jar包:spring-webmvc-5.3.10.jar; 赠送原API文档:spring-webmvc-5.3.10-javadoc.jar; 赠送源代码:spring-webmvc-5.3.10-sources.jar; 赠送Maven依赖信息文件:spring-webmvc-5.3.10.pom; 包含翻译后的API...
内容概要:该资源是Spring全家桶视频课程的第二部分Spring MVC的源码,其对于Spring的最常用的基本功能,如:表单、国际化、过滤器、拦截器、log等都使用案例进行了说明,并且在最后通过一个贯穿前、中、后台的案例...
赠送jar包:spring-webmvc-5.3.15.jar; 赠送原API文档:spring-webmvc-5.3.15-javadoc.jar; 赠送源代码:spring-webmvc-5.3.15-sources.jar; 赠送Maven依赖信息文件:spring-webmvc-5.3.15.pom; 包含翻译后的API...
《互联网轻量级SSM框架解密:Spring、Spring MVC、MyBatis源码深度剖析》以SSM 的核心代码剖析为基础,突破Java Web 研发瓶颈的束缚,选取Spring、SpringMVC 和MyBatis 框架中易于理解的版本,深入剖析了其中各个...
弹簧示例spring mvc项目的各种小例子
赠送jar包:spring-webmvc-5.3.7.jar; 赠送原API文档:spring-webmvc-5.3.7-javadoc.jar; 赠送源代码:spring-webmvc-5.3.7-sources.jar; 赠送Maven依赖信息文件:spring-webmvc-5.3.7.pom; 包含翻译后的API文档...