Spring Boot 与 JPA 增加内存使用量

问题描述 投票:0回答:1

我正在使用 Spring Boot Web 应用程序运行 JPA 教程代码。

我的 POM 是

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>xxx</modelVersion>
<groupId>xxxx</groupId>
<artifactId>xxx</artifactId>
<version>xxx</version>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.RELEASE</version>
    <relativePath />
</parent>

<properties>
    <start-class>com.Main</start-class>

    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <!-- https://mvnrepository.com/artifact/junit/junit -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc -->

    <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
    </dependency>
    <dependency>
        <groupId>javax.persistence</groupId>
        <artifactId>javax.persistence-api</artifactId>
        <version>2.2</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>eclipselink</artifactId>
        <version>2.6.3</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>com.microsoft.sqlserver</groupId>
        <artifactId>mssql-jdbc</artifactId>
        <scope>runtime</scope>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
我只坚持一门课:
@Entity
@JsonInclude(Include.NON_NULL)
public class TransactionData {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(unique=true)
private String referenceId;

public String getReferenceId() {
    return referenceId;
}

public void setReferenceId(String referenceId) {
    this.referenceId = referenceId;
}

我的应用程序属性是:

spring.datasource.url= jdbc:sqlserver://xxx.xxx.xxx.xxx:xxxx;databaseName=xxxxxx
spring.datasource.username= xxx
spring.datasource.password= xxx
spring.jpa.hibernate.dialect=org.hibernate.dialect.SQLServerDialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update

休息控制器:

@Autowired
TDataService tDataService;

@RequestMapping("/initTx")
@ResponseBody
public String initTx(String atmId, String refId) {
    TransactionData tD=new TransactionData();
    tD.setReferenceId(refId);
    return tDataService.save(tD).getReferenceId();
}

服务:

@Autowired
TDataRepo repository;

public TransactionData save(TransactionData tData) {
    return repository.save(tData);
}

存储库:

public interface TDataRepo  extends JpaRepository<TransactionData, Long> 

Repository 实现了 JpaRepository 中提供的 save 方法,无需修改。

一旦我使用Web应用程序启动tomcat,我就会执行简单的负载压力测试。我正在对控制器运行 1000 个休息调用,一次创建 1 个到 dB 的插入。通话之间有 1 秒延迟。

测试过程中,RAM不断增加,这是预料之中的,但从未释放。完成这些 1k 插入后,我不会向 Web 控制器发送任何请求,但 ram 保持不变,永远不会下降到原始空闲值。

我在这里遗漏了一些重要的东西吗?

spring-boot jpa memory-leaks
1个回答
0
投票

我不认为那里存在“内存泄漏”。我认为这与JVM如何管理内存有关。简而言之,它不会立即将未使用的内存返回给操作系统。您可以在这里找到一些提示GC是否将内存释放给操作系统?

© www.soinside.com 2019 - 2024. All rights reserved.