Spring-boot application to demo JVM HEAP and Native memory leak

Overview

Description

This repo can be used as demo repo for finding memory leaks.

Example spring-boot project to show how to find and fix

  1. JVM HEAP memory leak
  2. Native memory leak from the .so shared library file written in C++

More explanation about the how to find memory leaks is explained in the blog post: BLOG_POST_URL

Application details

The Application contains 2 rest end points

  1. To demonstrate how JVM HEAP memory leak looks like
Method: POST
URL: http://localhost:8080/users/heap
Example Body:

{
    "first_name" : "Iranna",
    "last_name" : "Patil",
    "email" : "[email protected]"
}
  1. To demonstrate Native shared library memory leak
Method: POST
URL: http://localhost:8080/users/native
Example Body:

{
   "first_name" : "Iranna",
   "last_name" : "Patil",
   "email" : "[email protected]"
}

Prerequisites

  1. Ubuntu(.so) and mac-os(.dylib)
  2. Java-15: OpenJDK-15
  3. Maven - 3.6.3
  4. g++ compiler for C++ code
  5. hey load generator: https://github.com/rakyll/hey
  6. jconsol from jdk
  7. Yourkit profiles for JVM application profiling: https://www.yourkit.com/
  8. jemalloc for process memory profiling: https://www.yourkit.com/

Running the Application.

Below are the steps to run the application.

  1. As the application makes JNI call, it needs shared library in the class path, so we need to build .so for the code written in C++. All the C++ code present in the folder "cpp", I have already generated header file for the JAVA native method code and also implemented corresponding logic in C++. Below are the steps to generate .so library file from C++ code using g++ compiler.
    1. Compile the CPP file
     g++ -c -fPIC -I{path}/jdk-15.0.2/include/ -I{path}/jdk-15.0.2/include/linux -I{path_to_cpp_files} {path_to_cpp_files}/datastore.cpp -o {output_directory_path}/datastore.o
    
    1. Convert the CPP compiled code to shared library`
    g++ -shared -fPIC -o {output_directory_path}/libdatastore.so {output_directory_path}/datastore.o -lc
    
    In the repo you can also find the .so file in folder cpp/lib/libdatastore, and this can be used for the demo if you have any issues in generating this file from C++ code.
  2. Once we generated the .so we can build our application jar file.
mvn clean install
  1. After generating JAR, now we can run the application and start sending the request. As we want to profile the whole process we want to use jemallo instead of glibc/libc to allocate memory. Please follow the steps to create jemalloc library and how to use https://github.com/jemalloc/jemalloc/wiki/ Once we have jemalloc built now we can start our application using the jemalloc and our own native code shared lib libdatastore.so as shown below. Open new terminal as do below steps.

    1. Make jemalloc as memory allocator instead of glibc/lic malloc
    export LD_PRELOAD=/usr/local/lib/libjemalloc.so
    

    Make sure you built jemalloc lib locally and placed it in above directory by following instructions from jemalloc documentation.

    1. Configure jemalloc so that it will write heap details of the process at every fixed interval to a file.
    export MALLOC_CONF=prof:true,lg_prof_interval:31,lg_prof_sample:17,prof_prefix:/tmp/heap/jeprof
    
    1. Set library path to folder were our .so native code library present so that JVM can call native methods, or else we will get Linking exceptions
    export LD_LIBRARY_PATH={output_directory_path}
    
    1. Now run the java application from the jar.
    java -Xmx500m -Xms250m -jar target/MemoryLeakDemo-1.0-SNAPSHOT.jar
    

We can check whether application is alive or not using alive endpoint http://localhost:8080/alive

Generating load

We can generate load using light weight async load generator following below steps.

  1. Create a user.json file having json content which will be used as body for requests.
user.json

{
    "first_name" : "Iranna",
    "last_name" : "Patil",
    "email" : "[email protected]"
}
  1. Now using this file we can generate load on any of the /users/heas or /users/native endpoint as shown below
hey -c 5 -z 20m -m POST -T "application/json" -D user.json http://localhost:8080/users/native

Checking memory leak

Heap

We can check the heap leak by connecting profiler to the application(Yourkit or jconsol)

Native

As we told the jemalloc to generate the heap details files in a directory, while starting the application. Now we can use jeprof to plot the graph or text file to show the memory details and also show the leak.

#!/bin/bash
jeprof --svg /tmp/heap/jeprof.$1.* >/tmp/heap/$1-report.svg 2>/dev/null
jeprof --text /tmp/heap/jeprof.$1.* >/tmp/heap/$1-report.txt 2>/dev/null

The parameter will be the porcess_id

Same script file can be found in this repo under folder profile

Once this svg and txt files generated, we can open them and see for memory leak details.

You might also like...

spring boot 实践学习案例,是 spring boot 初学者及核心技术巩固的最佳实践。另外写博客,用 OpenWrite。

spring boot 实践学习案例,是 spring boot 初学者及核心技术巩固的最佳实践。另外写博客,用 OpenWrite。

推荐工具: 微信公众号 Markdown 编辑器 - OpenWrite:Markdown 微信编辑器是一款专业强大的微信公众平台在线编辑排版工具,提供手机预览功能,让用户在微信图文 、文章、内容排版、文本编辑、素材编辑上更加方便。 - 更多介绍 博客群发平台 一、支持泥瓦匠 Spring Boot

Jan 5, 2023

Spring-Boot-Plus is a easy-to-use, high-speed, high-efficient,feature-rich, open source spring boot scaffolding

Spring-Boot-Plus is a easy-to-use, high-speed, high-efficient,feature-rich, open source spring boot scaffolding

Everyone can develop projects independently, quickly and efficiently! What is spring-boot-plus? A easy-to-use, high-speed, high-efficient, feature-ric

Dec 31, 2022

Spring Boot Migrator (SBM) - a tool for automated code migrations to upgrade or migrate to Spring Boot

Spring Boot Migrator (SBM) - a tool for automated code migrations to upgrade or migrate to Spring Boot

Spring Boot Migrator uses and is compatible to OpenRewrite, a powerful mass refactoring ecosystem for Java and other source code.

Jan 2, 2023

Spring Boot & MongoDB Login and Registration example with JWT, Spring Security, Spring Data MongoDB

Spring Boot & MongoDB Login and Registration example with JWT, Spring Security, Spring Data MongoDB

Spring Boot Login and Registration example with MongoDB Build a Spring Boot Auth with HttpOnly Cookie, JWT, Spring Security and Spring Data MongoDB. Y

Dec 30, 2022

source code of the live coding demo for "Building resilient and scalable API backends with Apache Pulsar and Spring Reactive" talk held at ApacheCon@Home 2021

reactive-iot-backend The is the source code of the live coding demo for "Building resilient and scalable API backends with Apache Pulsar and Spring Re

Jan 13, 2022

一个涵盖六个专栏:Spring Boot 2.X、Spring Cloud、Spring Cloud Alibaba、Dubbo、分布式消息队列、分布式事务的仓库。希望胖友小手一抖,右上角来个 Star,感恩 1024

一个涵盖六个专栏:Spring Boot 2.X、Spring Cloud、Spring Cloud Alibaba、Dubbo、分布式消息队列、分布式事务的仓库。希望胖友小手一抖,右上角来个 Star,感恩 1024

友情提示:因为提供了 50000+ 行示例代码,所以艿艿默认注释了所有 Maven Module。 胖友可以根据自己的需要,修改 pom.xml 即可。 一个涵盖六个主流技术栈的正经仓库: 《Spring Boot 专栏》 《Spring Cloud Alibaba 专栏》 《Spring Clou

Dec 31, 2022

参考 DDD/Clean Architecture 设计理念,整合 Spring Boot/Spring Security/Mybatis Plus/Vavr 的 Spring Realworld 应用案例

参考 DDD/Clean Architecture 设计理念,整合 Spring Boot/Spring Security/Mybatis Plus/Vavr 的 Spring Realworld 应用案例

Demo · 更多项目 · 参考资料 ms-spring-ddd-examples Unified Domain-driven Layered Architecture for MicroService Apps,试图探索一套切实可行的应用架构规范,可以复制、可以理解、可以落地、可以控制复杂性的指导

Sep 23, 2022

Spring Kurulumundan Başlayarak, Spring IOC ve Dependency Injection, Hibernate, Maven ve Spring Boot Konularına Giriş Yapıyoruz.

Spring Kurulumundan Başlayarak, Spring IOC ve Dependency Injection, Hibernate, Maven ve Spring Boot Konularına Giriş Yapıyoruz.

Spring Tutorial for Beginners File Directory Apache Tomcat Apache Tomcat - Eclipse Bağlantısı Spring Paketlerinin İndirilmesi ve Projeye Entegrasyonu

Apr 11, 2022

Spring Boot JdbcTemplate example with SQL Server: CRUD Rest API using Spring Data JDBC, Spring Web MVC

Spring Boot JdbcTemplate example with SQL Server: Build CRUD Rest API Build a Spring Boot CRUD Rest API example that uses Spring Data Jdbc to make CRU

Dec 20, 2022
Owner
Iranna Nk
love to code :)
Iranna Nk
循序渐进,学习Spring Boot、Spring Boot & Shiro、Spring Batch、Spring Cloud、Spring Cloud Alibaba、Spring Security & Spring Security OAuth2,博客Spring系列源码:https://mrbird.cc

Spring 系列教程 该仓库为个人博客https://mrbird.cc中Spring系列源码,包含Spring Boot、Spring Boot & Shiro、Spring Cloud,Spring Boot & Spring Security & Spring Security OAuth2

mrbird 24.8k Jan 6, 2023
Demo microservice architecture with Spring ,Spring Cloud Gateway , Spring Cloud config server , Eureuka , keycloak and Docker.

spring-microservice Demo microservice architecture with Spring ,Spring Cloud Gateway , Spring Cloud config server , Eureuka , keycloak and Docker. Arc

null 4 Sep 13, 2022
Two Spring-boot applications registering themselves to an spring-boot-admin-server application as separate clients for the purpose of monitoring and managing the clients

Spring-boot-admin implementation with 1 Server and 2 clients Creating a Server application to monitor and manage Spring boot applications (clients) un

null 6 Dec 6, 2022
Spring Boot Login and Registration example with MySQL, JWT, Rest Api - Spring Boot Spring Security Login example

Spring Boot Login example with Spring Security, MySQL and JWT Appropriate Flow for User Login and Registration with JWT Spring Boot Rest Api Architect

null 58 Jan 5, 2023
Microservices with Spring Boot and Kafka Demo Project

Example microservices showing how to use Kafka and Kafka Streams with Spring Boot on the example of distributed transactions implementations with the SAGA pattern

Piotr Mińkowski 98 Jan 7, 2023
Spring Boot ECommerce Demo

springboot-ecommerce2 Things todo list Clone this repository: git clone https://github.com/hendisantika/springboot-ecommerce2.git Navigate to the fold

Hendi Santika 5 Aug 30, 2022
Spring JPA Native Query example in Spring Boot

Spring JPA Native Query example in Spring Boot

null 12 Nov 30, 2022
Rise SRC Leak, original leaker had his github deleted :(

Hope i don't get deleted too Original REAMDE.md: leaked by mika johans intent guard obfuscation bypass 2021 zip exploited auth to get the SRC ##Permis

null 10 May 6, 2022
The Spring Boot Sample App on K8S has been implemented using GKE K8S Cluster, Spring Boot, Maven, and Docker.

gke-springboot-sampleapp ?? The Spring Boot Sample App on K8S has been implemented using GKE K8S Cluster, Spring Boot, Maven, and Docker. Usage To be

KYEONGMIN CHO 1 Feb 1, 2022
about learning Spring Boot via examples. Spring Boot 教程、技术栈示例代码,快速简单上手教程。

Spring Boot 学习示例 Spring Boot 使用的各种示例,以最简单、最实用为标准,此开源项目中的每个示例都以最小依赖,最简单为标准,帮助初学者快速掌握 Spring Boot 各组件的使用。 Spring Boot 中文索引 | Spring Cloud学习示例代码 | Spring

纯洁的微笑 28.3k Jan 1, 2023