内存泄漏Springboot

3 java spring hibernate jpa

我有一个似乎使用过多内存的应用程序。我已经尝试寻找源了一段时间。但是仍然没有运气。

我已经阅读了几篇文章,这些文章指出JPA是Spring Boot的某些内存问题的元凶。我只有一个存储库,所以我无法想象这是问题所在。

@Repository
public interface  WordRepository extends JpaRepository<Word, Long> {

    @Query("SELECT w FROM Word w WHERE w.value IN (:words)")
    List<Word> findAllIn(@Param("words") List<String> words);

    Word findFirstByValue(String value);

    @Transactional
    Long removeByValue(String value);

    Page<Word> findAllByCategory(Pageable pageable, String category);
}
Run Code Online (Sandbox Code Playgroud)

我还有另一个类,可以用来删除表。我无法使用JPA(我知道)执行此操作,因此我获得了持久性对象并使用它来截断表。

@Service
public class WordRepositoryHelper {

    @PersistenceContext(unitName = "default")
    private EntityManager entityManager;

    @Transactional
    public int truncateWordTable() {
        final String sql = "truncate table word";
        entityManager.createNativeQuery(sql).executeUpdate();
        return 1;
    }
}
Run Code Online (Sandbox Code Playgroud)

我在这里使用它们。

@Service
@SuppressWarnings("deprecation")
public class CSVResourceService implements ResourceService {

    @Autowired
    private WordRepository wordRepository;

    @Autowired
    private WordRepositoryHelper wordRepositoryHelper;

    private static final String HEADER_ID = "id";
    private static final String HEADER_VALUE = "value";
    private static final String HEADER_CATEGORY = "category";

    @Override
    public Boolean save(MultipartFile file, Boolean replace) throws Exception {

        // some other code

        if (replace) {
            wordRepositoryHelper.truncateWordTable();
        }

        //some other code
    }
}
Run Code Online (Sandbox Code Playgroud)

关于该问题或建议的任何指导。

小智 6

非常感谢评论中的所有建议。您可能已经猜到这是我第一次处理内存问题,并且由于它是在生产环境中发生的,因此我感到有些恐慌。

因此,真正的问题不是真正的JPA。它更像是问题和问题的结合。就像一些评论所建议的那样,我的记忆力问题应归咎于很多候选人:

  • JPA
  • 提卡
  • Opencv的
  • Tesseract

这是我解决问题的方法:

  1. 教育。到那里去学习一些有关该问题以及如何解决的知识。这是我使用的一些链接:
    https : //www.toptal.com/java/hunting-memory-leaks-in-java
    https://developers.redhat.com/blog/2014/08/14/find-fix -memory-leaks-java-application /
    https://app.pluralsight.com/player?course=java-understanding-solving-memory-problems
  2. 使用新知识并绘制一些图表,了解您的内存状况。相信我,这次探索揭示了一大堆我不知道正在发生的事情。(就像一些汉字数组一样,我猜Tesseract需要将其用于OCR)
    在此处输入图片说明
    在此处输入图片说明
  3. 然后-Xms256m -Xmx512m在执行罐子时使用,这样您就可以缩小范围并查看应归咎于谁。同样,它将限制服务器中的资源。
  4. 所有这些导致我一般有两种可能的泄漏和麻烦原因。
    1)使用POI或Tika提取文本,以流到流的方式而不将整个文件加载到内存中
    2)迭代Opencv帧导致内存泄漏

就是这样,我认为我现在很好。

感谢所有人的帮助。