当停止/重新部署应用程序时,我遇到了Tomcat内存泄漏问题。它说:以下Web应用程序已停止(重新加载,未部署),但是它们之前运行时加载的类仍然存在于内存中,从而导致内存泄漏(请使用分析工具进行确认):/test-1.0-SNAPSHOT。
MySQL连接器驱动程序位于Tomcat/lib文件夹中。我可以在Tomcat 7/8两个版本中复现此问题。还尝试了使用"net.sourceforge.jtds.*"驱动程序进行MS SQL数据库操作,但没有帮助。
请查找以下项目文件。该项目仅在数据库中创建1个表格。
build.gradle
group 'com.test'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'war'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile group: 'org.hibernate', name: 'hibernate-entitymanager', version: '5.2.10.Final'
compile group: 'org.springframework.data', name: 'spring-data-jpa', version: '1.11.4.RELEASE'
compile group: 'org.springframework', name: 'spring-webmvc', version: '4.3.9.RELEASE'
providedCompile 'javax.servlet:javax.servlet-api:3.1.0'
providedCompile group: 'mysql', name: 'mysql-connector-java', version: '5.1.6'
compile group: 'commons-dbcp', name: 'commons-dbcp', version: '1.4'
}
ApplicationConfig.java
@Configuration
@Import({JPAConfiguration.class})
@EnableWebMvc
public class ApplicationConfig {}
JPAConfiguration.java
@Configuration
@EnableJpaRepositories("com.test.dao")
@EnableTransactionManagement
public class JPAConfiguration {
@Bean
public EntityManagerFactory entityManagerFactory() {
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
factory.setPackagesToScan("com.test.model");
factory.setDataSource(restDataSource());
factory.setJpaPropertyMap(getPropertyMap());
factory.afterPropertiesSet();
return factory.getObject();
}
@Bean(destroyMethod = "close")
public DataSource restDataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/test");
dataSource.setUsername("test");
dataSource.setPassword("test");
return dataSource;
}
private Map<String, String> getPropertyMap() {
Map<String, String> hibernateProperties = new HashMap<>();
hibernateProperties.put("hibernate.hbm2ddl.auto", "update");
hibernateProperties.put("hibernate.show_sql", "true");
hibernateProperties.put("hibernate.format_sql", "true");
hibernateProperties.put("hibernate.dialect", "org.hibernate.dialect.MySQL5InnoDBDialect");
return hibernateProperties;
}
@Bean
public PlatformTransactionManager transactionManager() {
JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setEntityManagerFactory(entityManagerFactory());
return txManager;
}
}
TestRepository.java
@Repository
public interface TestRepository extends JpaRepository<TestEntity, Long> {}
TestEntity.java
@Entity
@Table(name = "ent")
public class TestEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String descript;
//equals, hashcode, toString, getters, setters
}
AppInitializer.java
public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
private WebApplicationContext rootContext;
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[]{ApplicationConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return null;
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
命令
jmap -histo <tomcat_pid>
在Tomcat停止后,项目结构只显示了2个项:
com.test.config.dao.JPAConfiguration$$EnhancerBySpringCGLIB$$792cb231$$FastClassBySpringCGLIB$$45ff499c
com.test.config.dao.JPAConfiguration$$FastClassBySpringCGLIB$$10104c1e
有人对解决这个问题有什么想法或建议吗?
onStartup
和createRootApplicationContext
,这样你会创建一个几乎没有用处的额外上下文并删除该字段。你试图变得太聪明了,而且这会干扰生命周期。我还建议删除不需要的setDriverClassName
行,因为JDBC可以根据URL基本上找出驱动程序。 - M. DeinumonStartup
和createRootApplicationContext
方法,但没有帮助。Tomcat仍然显示有关内存泄漏的消息。尝试删除setDriverClassName
但出现异常org.hibernate.engine.jdbc.spi.SqlExceptionHelper.logExceptions Cannot create JDBC driver of class "" for connect URL "jdbc:mysql://localhost:3306/test"
。 - Eugene Gotovko