在项目开发过程中,遇到一个jvm 不能回收内存的问题 ,导致到最后old区占100%,服务器一直超时响应很慢,最后自动重启。。。。。
通过jstack 查看得到 线程有6-700个线程全部处于阻塞状态:BLOCKED
"resin-tcp-connection-*:8080-2401" daemon prio=10 tid=0x00002aab3df66000 nid=0x1693 waiting for monitor entry [0x00002aac50f09000]
java.lang.Thread.State: BLOCKED (on object monitor)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:180)
- waiting to lock <0x00002aaabe5fd790> (a java.util.concurrent.ConcurrentHashMap)
at org.springframework.beans.factory.support.AbstractBeanFactory.isTypeMatch(AbstractBeanFactory.java:415)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:223)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:297)
at org.springframework.context.support.AbstractApplicationContext.getBeansOfType(AbstractApplicationContext.java:941)
at org.springframework.beans.factory.BeanFactoryUtils.beansOfTypeIncludingAncestors(BeanFactoryUtils.java:224)
at org.springframework.beans.factory.BeanFactoryUtils.beanOfTypeIncludingAncestors(BeanFactoryUtils.java:309)
具体错误见上面,查看了一下spring的源代码
protected Object getSingleton(String beanName, boolean allowEarlyReference) {
Object singletonObject = this.singletonObjects.get(beanName);
if (singletonObject == null) {
synchronized (this.singletonObjects) {
singletonObject = this.earlySingletonObjects.get(beanName);
if (singletonObject == null && allowEarlyReference) {
ObjectFactory singletonFactory = (ObjectFactory) this.singletonFactories.get(beanName);
if (singletonFactory != null) {
singletonObject = singletonFactory.getObject();
this.earlySingletonObjects.put(beanName, singletonObject);
this.singletonFactories.remove(beanName);
}
}
}
}
return (singletonObject != NULL_OBJECT ? singletonObject : null);
}
网上这是spring的一个缺陷,但直到最新版本也没有见修复。具体见如URL:
https://jira.springsource.org/browse/SPR-6870
Spring中 获取一个spring容器管理的Bean可以通过 getBeanByName or getBeanByType
其中能过前者获取到的beanspring是有加缓存的,而getBeanByType 每次都是重新去生成(其实我也没去深究过他的源码)
不过我做了一个测试:
long startTime = System.currentTimeMillis();
User user3 = null;
for(int i=0; i<100000; i++) {
user3 = BeanFactory.getBean("user");
user3.print();
}
long endTime = System.currentTimeMillis();
System.out.println("getBeanByName spend time :" + (endTime - startTime) + "ms");
System.out.println("current momory:" + SprintTest.getUsedMemory());
User user4 = null;
for(int i=0; i<100000; i++) {
user4 = BeanFactory.getBean(User.class);
user4.print();
}
long end = System.currentTimeMillis();
System.out.println("getBeanBytype spend time :" + (end - endTime) + "ms");
System.out.println("current momory:" + SprintTest.getUsedMemory());
Factory方法:
@SuppressWarnings("unchecked")
public static <T> T getBean(final String name) {
return (T) ctx.getBean(name);
}
public static <T> T getBean(final Class<T> clazz) {
return clazz.cast(BeanFactoryUtils.beanOfTypeIncludingAncestors(ctx, clazz));
}
结果如下:
getBeanByName spend time :86ms
current momory:899560
getBeanBytype spend time :773ms
current momory:1576440
很震撼。。。。时间直接是数量级的差别,空间也有很大的差别。
建议大家尽量少使用getBeanByType 吧,如果一定需要用的话,可以通过人为的加缓存来解决这种性能问题 ,尽量少使用避免这种BUG出现。
此外还引出另外一个问题 ,有的同学在Util里面引用一些Sprint管理的类,一般Util的方法都是静态的,他本身不在Sprint容器的管理范围之内,那怎么解决这个方法呢,可以用Sprng工厂去获得bean
public class BeanFactory implements ApplicationContextAware {
private static ApplicationContext ctx;
private static BeanFactory singleton;
public BeanFactory() {
singleton = this;
}
public static BeanFactory getInstance(){
return singleton;
}
public void setApplicationContext(ApplicationContext ctx)
throws BeansException {
BeanFactory.ctx = ctx;
}
@Autowired
private User user;
public User getUser() {
return user;
}
@SuppressWarnings("unchecked")
public static <T> T getBean(final String name) {
return (T) ctx.getBean(name);
}
public static <T> T getBean(final Class<T> clazz) {
return clazz.cast(BeanFactoryUtils.beanOfTypeIncludingAncestors(ctx, clazz));
}
可以通过BeanFactory.getInstance().getBean("name") 的方法去实现
如果是在Spring管理的bean里面调用别的bean的话就简了 或者你通过Spring 的@Autowired注入,或者set 注入 都可以。
分享到:
相关推荐
Classes contained in spring-mock.jar: org.springframework.mock.jndi.ExpectedLookupTemplate.class org.springframework.mock.jndi.SimpleNamingContext.class org.springframework.mock.jndi....
org.springframework.flex-1.0.3.RELEASE.jar.zip用于JAR包,org.springframework.flex-1.0.3.RELEASE.jar.zip用于JAR包org.springframework.flex-1.0.3.RELEASE.jar.zip用于JAR包org.springframework.flex-1.0.3....
1.精通Spring 4.x 企业应用开发实战精通Spring 4.x 企业应用开发实战精通Spring 4.x 企业应用开发实战精通Spring 4.x 企业应用开发实战精通Spring 4.x 企业应用开发实战精通Spring 4.x 企业应用开发实战精通Spring 4...
org.springframework.remoting.caucho.BurlapClientInterceptor.class org.springframework.remoting.caucho.BurlapProxyFactoryBean.class org.springframework.remoting.caucho.BurlapServiceExporter.class org....
org.springframework.aop-3.0.4.RELEASE.jar org.springframework.asm-3.0.4.RELEASE.jar org.springframework.aspects-3.0.4.RELEASE.jar org.springframework.beans-3.0.4.RELEASE.jar org.springframework....
spring4.x中的jar包下载,spring4.0.6下载,spring最新稳定版jar包下载 http://maven.springframework.org/release/org/springframework/spring/ 这个链接中有各种稳定版的jar包下载 目前官网上大部分都要maven下载
Error creating bean with name 'org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#0' defined in ServletContext resource [/WEB-INF/springMVC-servlet.xml]: Initialization of bean failed;...
aopalliance-1.0.jar,org.springframework.aop-3.0.0.RELEASE.jar,org.springframework.jdbc-3.0.0.RELEASEorg.springframework.beans-3.0.0.RELEASE.jar等
org.springframework.web.jar,没问题的,最新的。
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
java解决org.springframework.web.context.ContextLoaderListener
spring3.1官方所有的jar包 org.springframework.aop-3.1.RELEASE.jar org.springframework.asm-3.1.RELEASE.jar org.springframework.aspects-3.1.RELEASE.jar org.springframework.beans-3.1.RELEASE.jar org....
spring4.x
org.springframework.web.struts-3.1.0.M2.jar
精通Spring 4.x 企业应用开发实战 陈雄华 PDF,清晰而且有标签,目录,非常好,谢谢分享。精通Spring 4.x 企业应用开发实战 陈雄华 PDF,清晰而且有标签,目录,非常好,谢谢分享。
org.springframework.core.jar
org.springframework.orm-3.0.0.M1.jar
jar包清单: aopalliance.jar aspectjweaver-1.8.2.jar commons-beanutils.jar commons-collections-3.1.jar commons-lang-2.3.jar commons-logging-1.1.3.jar ...org.springframework.web-3.0.0.RELEASE.jar
Spring4.X教学视频,教会你如何一步一步搭建spring项目。