简体   繁体   中英

Java JAR took High CPU Usage and Memory consumption

I am running a jar file on ubuntu system having 16 GB RAM. I used spring boot framework, JPA while developing the application. I observed that memory and cpu usage keep on increasing after application started.

What could be possible reason to constantly increase the CPU usage and memory ?

What are operations is doing by application -> Update text file with

    File file = new File(file path);
    OutputStream os = new FileOutputStream(file.getAbsoluteFile());

and read operation by

    FileInputStream inputStream = new FileInputStream(file);
    InputStreamResource resource = new InputStreamResource(inputStream);

updating file with one min frequency.

I tried with run the jar as well as deploy war file on tomcat server in both cases its behaving same.

The main intension is to create proto-buffer text file. I have added

@Scheduled(fixedDelay = 60000)
@RequestMapping(value = "/test")
public String generateProtoBuf(){
...
}  

so every min this method will trigger and update the text file.

I exposed another API.

@RequestMapping(path = "/getProtoBuf", method = RequestMethod.GET)
public ResponseEntity<Resource> getProtoBuf() throws IOException {
...
}

this API will call every twice in min. file size will be less than 1 MB.

For initial few hours app is running normally but after few hour when cpu usage increase by 15% then It will drastically increase and touches 100% .

In your case, creating and (automatically) garbage collecting big object can cause a higher cpu usage, than working with less big objects. Also, when having a bad IO performance, it can also slow higher your CPU usage, when having heavy IO operations.

But mostly unperformant code will cause a high CPU usage. Check your code, or find the bottlenecks with a Java profiler. I can recomment YourKit , since it is free and easy to use. But there are many other tools, which achieve the same result. When running your application with the profiler, you will see the functions/methods, which cause the high CPU usage.

I would recommend you to a find a thread that hogging CPU; you can do that with Arthas open source tool from Alibaba

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM