spring 4 mvc + Hibernate + Annotations 配置堆内存问题

Posted

技术标签:

【中文标题】spring 4 mvc + Hibernate + Annotations 配置堆内存问题【英文标题】:spring 4 mvc + Hibernate + Annotations configuration Heap memory issue 【发布时间】:2017-06-28 06:42:37 【问题描述】:

环境细节是。

Intellij IDE
Spring 4
Hibernate with simple schema of 30 tables
Elastic search client
mysql
CRUD operation repository classes

所以我的问题是在应用程序启动时内存达到 950MB,这对我来说看起来很不正常。

我正在使用基于注释的配置。

@Configuration
@Import(ElasticSearchConfig.class)
@PropertySources(
        @PropertySource("classpath:application.properties"),
        @PropertySource("classpath:sms_templates.xml")
    )
@ComponentScan(basePackages = "com.company", excludeFilters = 
        @ComponentScan.Filter(value = Controller.class, ControllerAdvice.class, RestController.class, type = FilterType.ANNOTATION),
        @ComponentScan.Filter(pattern = "com\\.company\\.web\\..*", type = FilterType.REGEX)
)
@EnableAspectJAutoProxy
@EnableCaching
@EnableAsync
public class RootConfiguration 

Maven 部门

<properties>
    <jackson.version>2.7.3</jackson.version>
    <spring_batch.version>3.0.5.RELEASE</spring_batch.version>
    <spring.version>4.3.2.RELEASE</spring.version>
    <spring_session.version>1.2.1.RELEASE</spring_session.version>
    <spring_security.version>4.1.3.RELEASE</spring_security.version>
    <java_mail.version>1.4</java_mail.version>
    <spring_data.version>1.10.2.RELEASE</spring_data.version>
    <hibernate.version>4.3.4.Final</hibernate.version>
    <maven.compiler.target>1.7</maven.compiler.target>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.test.skip>true</maven.test.skip>
    <java-version>1.7</java-version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.12</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>$jackson.version</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>$jackson.version</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>$jackson.version</version>
    </dependency>
    <dependency>
        <groupId>com.googlecode.json-simple</groupId>
        <artifactId>json-simple</artifactId>
        <version>1.1</version>
    </dependency>
    <dependency>
        <groupId>p6spy</groupId>
        <artifactId>p6spy</artifactId>
        <version>2.3.0</version>
    </dependency>
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.3.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-io</artifactId>
        <version>1.3.2</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>$spring.version</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.batch</groupId>
        <artifactId>spring-batch-core</artifactId>
        <version>$spring_batch.version</version>
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.6.2</version>
    </dependency>
    <dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch</artifactId>
        <version>2.3.4</version>
    </dependency>
    <dependency>
        <groupId>one.util</groupId>
        <artifactId>streamex</artifactId>
        <version>0.6.2</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>$spring.version</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>$spring.version</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>$spring.version</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>$spring.version</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>$spring.version</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>$spring.version</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>$spring.version</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>$spring.version</version>
        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.session</groupId>
        <artifactId>spring-session-data-redis</artifactId>
        <version>$spring_session.version</version>
    </dependency>
    <dependency>
        <groupId>com.github.kstyrc</groupId>
        <artifactId>embedded-redis</artifactId>
        <version>0.6</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
        <version>$spring_security.version</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
        <version>$spring_security.version</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-taglibs</artifactId>
        <version>$spring_security.version</version>
    </dependency>

    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>mail</artifactId>
        <version>$java_mail.version</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.1</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.1.2</version>
    </dependency>
    <dependency>
        <groupId>taglibs</groupId>
        <artifactId>standard</artifactId>
        <version>1.1.2</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>$spring_data.version</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-elasticsearch</artifactId>
        <version>2.0.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-dbcp2</artifactId>
        <version>2.0</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>$hibernate.version</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>$hibernate.version</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.1-api</artifactId>
        <version>1.0.0.Final</version>
    </dependency>
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>1.0.0.GA</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>5.2.4.Final</version>
    </dependency>
    <dependency>
        <groupId>commons-beanutils</groupId>
        <artifactId>commons-beanutils</artifactId>
        <version>1.9.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.1</version>
    </dependency>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.4</version>
    </dependency>
    <dependency>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.2</version>
    </dependency>
    <dependency>
        <groupId>net.sf.ehcache</groupId>
        <artifactId>ehcache</artifactId>
        <version>2.10.3</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.9</version>
    </dependency>
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>1.1.0.Final</version>
    </dependency>
</dependencies>

谁能告诉我这里的问题,为什么它会如此高的堆使用率。除了轻量级属性文件之外,我没有加载任何数据。

22:52:49,896  INFO localhost-startStop-1 context.ContextLoader:305 - Root WebApplicationContext: initialization started
22:52:49,905  INFO localhost-startStop-1 support.AnnotationConfigWebApplicationContext:581 - Refreshing Root WebApplicationContext: startup date [Thu Feb 09 22:52:49 EST 2017]; root of context hierarchy
22:52:50,046  INFO localhost-startStop-1 support.AnnotationConfigWebApplicationContext:207 - Registering annotated classes: [class com.company.config.RootConfiguration,class com.company.config.StandaloneRedisConfiguration]
22:52:50,685  INFO localhost-startStop-1 config.RepositoryConfigurationDelegate:166 - Multiple Spring Data modules found, entering strict repository configuration mode!
22:52:51,395  INFO localhost-startStop-1 config.RepositoryConfigurationDelegate:166 - Multiple Spring Data modules found, entering strict repository configuration mode!
22:52:51,420  INFO localhost-startStop-1 xml.XmlBeanDefinitionReader:317 - Loading XML bean definitions from class path resource [security-context.xml]
22:52:51,683  INFO localhost-startStop-1 core.SpringSecurityCoreVersion:75 - You are running with Spring Security Core 4.1.3.RELEASE
22:52:51,683  INFO localhost-startStop-1 config.SecurityNamespaceHandler:78 - Spring Security 'config' module version is 4.1.3.RELEASE
22:52:51,755  INFO localhost-startStop-1 method.GlobalMethodSecurityBeanDefinitionParser:207 - Expressions were enabled for method security but no SecurityExpressionHandler was configured. All hasPermision() expressions will evaluate to false.
22:52:53,340  INFO localhost-startStop-1 config.RootConfiguration:47 - Messages loaded.
22:52:54,236  INFO localhost-startStop-1 jpa.LocalContainerEntityManagerFactoryBean:339 - Building JPA container EntityManagerFactory for persistence unit 'default'
22:52:54,415  INFO localhost-startStop-1 hibernate.Version:54 - HHH000412: Hibernate Core 4.3.4.Final
22:52:54,419  INFO localhost-startStop-1 cfg.Environment:239 - HHH000206: hibernate.properties not found
22:52:54,423  INFO localhost-startStop-1 cfg.Environment:346 - HHH000021: Bytecode provider name : javassist
22:52:54,938  INFO localhost-startStop-1 common.Version:66 - HCANN000001: Hibernate Commons Annotations 4.0.4.Final
22:52:55,741  INFO localhost-startStop-1 dialect.Dialect:145 - HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect
22:52:55,773  INFO localhost-startStop-1 internal.LobCreatorBuilder:97 - HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
22:52:56,610  INFO localhost-startStop-1 ast.ASTQueryTranslatorFactory:47 - HHH000397: Using ASTQueryTranslatorFactory
22:52:56,742  INFO localhost-startStop-1 util.Version:30 - HV000001: Hibernate Validator 5.2.4.Final
22:52:58,866  INFO localhost-startStop-1 hbm2ddl.SchemaUpdate:182 - HHH000228: Running hbm2ddl schema update
22:52:58,866  INFO localhost-startStop-1 hbm2ddl.SchemaUpdate:193 - HHH000102: Fetching database metadata
22:52:58,888  INFO localhost-startStop-1 hbm2ddl.SchemaUpdate:205 - HHH000396: Updating schema

【问题讨论】:

错误信息是什么??如果你在像 tomcat 这样的服务器上运行它,请增加 permgen 空间。 我没有收到错误消息我想知道为什么它会占用这么多内存,我将部署在 2GB 内存服务器上,这样会出现问题,让我担心。 它跳得太高了。但是这种没有硬数字的问题(使用分析器很容易得到)似乎是一场野鹅追逐 【参考方案1】:

尝试附加 jvisualvm 和 Goto Profiler --> 内存选项卡或示例 --> 内存。您可能会看到一些不应该以如此高的数量出现的对象。或者进行内存转储并使用堆转储分析器。这将使您更清楚地了解正在发生的事情

【讨论】:

【参考方案2】:

我可以保证您的应用程序所做的不仅仅是加载一些轻量级的属性文件。当 spring boot 在 classpath 上看到库时,它会自动加载默认配置并生成一系列 bean。

内存的增加很可能正是这种情况的产物。

如果你觉得有必要,你当然可以确定。

【讨论】:

这对我来说很有意义,但是除了我的应用程序类之外,还有什么方法可以限制生成一系列 bean。我想我将组件扫描限制在注释中的 com.company 包中

以上是关于spring 4 mvc + Hibernate + Annotations 配置堆内存问题的主要内容,如果未能解决你的问题,请参考以下文章

从 Migrate 迁移到 Spring MVC 4 + Hibernate5

通用后台管理系统(ExtJS 4.2 + Spring MVC 3.2 + Hibernate)

Spring+Spring MVC+Hibernate框架搭建实例

杰克逊 JSON、Spring MVC 4.2 和 Hibernate JPA 问题的无限递归

spring mvc+spring + hibernate 整合

spring mvc+spring + hibernate 整合