Java Spring启动内存利用率

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24129446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 10:20:58  来源:igfitidea点击:

Spring boot memory utilization

javaspringspring-boot

提问by DilTeam

We have an application based on Spring Boot. We deploy it using an executable jar which is about 20Meg. When we start this application, it uses 18Gigs worth of "Virutual" memory right away. I understand that most of this is kept in secondary storage such as hard disk, but our System Administrators are questioning why we need so much of Virtual memory.

我们有一个基于 Spring Boot 的应用程序。我们使用一个大约 20Meg 的可执行 jar 来部署它。当我们启动这个应用程序时,它立即使用了 18Gigs 的“虚拟”内存。我知道其中大部分都保存在二级存储中,例如硬盘,但我们的系统管理员质疑为什么我们需要这么多虚拟内存。

The Jar file is small. At the startup I am using BoneCPDataSource but I've cut down # of connections to 5. As soon as application is started it uses 18g of Virtual memory.

Jar 文件很小。在启动时,我使用 BoneCPDataSource,但我已将连接数减少到 5。一旦应用程序启动,它就会使用 18g 的虚拟内存。

Can someone tell me why? Should I play with java memory settings on command line to reduce this number? The command we use is:

有人能告诉我为什么吗?我应该在命令行上使用 java 内存设置来减少这个数字吗?我们使用的命令是:

java -jar filename.jar

java -jar 文件名.jar

As requested, here's some more info:

根据要求,这里有更多信息:

1) Here's a line from 'top' command:

24511 xyz 20 0 17.6g 531m 9.8m S 0.0 0.8 0:16.57 java

1)这是'top'命令中的一行:

24511 xyz 20 0 17.6g 531m 9.8m S 0.0 0.8 0:16.57 java

2) There's no Neo4J. Here's my pom.xml

2) 没有 Neo4J。这是我的 pom.xml

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

<groupId>com.xyz.myapp</groupId>
<artifactId>myapp-rest-service</artifactId>
<version>0.1.0</version>

<properties>
    <start-class>com.xyz.myapp.rest.Application</start-class>
</properties>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.0.1.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>com.jolbox</groupId>
        <artifactId>bonecp</artifactId>
        <version>0.8.0.RELEASE</version>
        <exclusions>
            <exclusion>
                <groupId>com.jolbox</groupId>
                <artifactId>com.jolbox.logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.Hymanson.core</groupId>
        <artifactId>Hymanson-databind</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>4.0.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>4.0.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.0-rc1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.0-rc1</version>
    </dependency>
    <dependency>
        <groupId>oracle</groupId>
        <artifactId>jdbc</artifactId>
        <version>6.0</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-bundle</artifactId>
        <version>1.18.1</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin> 
            <artifactId>maven-compiler-plugin</artifactId> 
            <version>2.3.2</version> 
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

<repositories>
    <repository>
        <id>spring-snapshots</id>
        <url>http://repo.spring.io/libs-snapshot</url>
        <snapshots><enabled>true</enabled></snapshots>
    </repository>
</repositories>

<pluginRepositories>
    <pluginRepository>
        <id>spring-snapshots</id>
        <url>http://repo.spring.io/libs-snapshot</url>
        <snapshots><enabled>true</enabled></snapshots>
    </pluginRepository>
</pluginRepositories>

3) I build this application using following command:

3)我使用以下命令构建此应用程序:

mvn clean package

mvn 清洁包

and this is what it creates in the ./target directory:

这是它在 ./target 目录中创建的内容:

-rwxr-xr-x 1 myuserid Domain Users 20259555 Jun 9 15:36 myapp-rest-service-0.1.0.jar

-rwxr-xr-x 1 myuserid 域用户 20259555 Jun 9 15:36 myapp-rest-service-0.1.0.jar

4) lsof output for my app:

4) 我的应用程序的 lsof 输出:

http://pastebin.com/V00BrD67

http://pastebin.com/V00BrD67

5) Output from mvn dependency:tree

5)mvn依赖的输出:树

http://pastebin.com/cFnR0NMX

http://pastebin.com/cFnR0NMX

采纳答案by DilTeam

After specifying -Xmxoption, the virtual memory consumption went down. Currently, I am starting my Spring Boot app like this:

指定-Xmx选项后,虚拟内存消耗下降。目前,我正在像这样启动我的 Spring Boot 应用程序:

java -Xmx4096m -jar myapp-rest-service-0.1.0.jar

If there's a better way to set this value in application.propertiesfile (or somewhere else), please do let me know.

如果有更好的方法在application.properties文件(或其他地方)中设置此值,请告诉我。

回答by Steve Oh

JVM uses default heap size based on the physical memory available (usually a percentage of available system memory). The value also depends on the JVM version and the client/server mode.

JVM 根据可用的物理内存(通常是可用系统内存的百分比)使用默认堆大小。该值还取决于 JVM 版本和客户端/服务器模式。

you can find more about this topic with these keyword: "default Java heap configuration"

您可以使用以下关键字找到有关此主题的更多信息:“默认 Java 堆配置”

this SO answers addresses the same topic (Jabir's answer): What is the default maximum heap size for Sun's JVM from Java SE 6?

这个 SO 答案解决了相同的主题(Jabir 的答案): Java SE 6 中 Sun JVM 的默认最大堆大小是多少?

HTH

HTH