java 性能调优总结

1,jenkin 构建脚本(cpu, 内存)配置是否符合要求,比如1core 3G
2,tomcat线程池是否配置恰当
3,数据库是否启用第三方的数据库连接池
4,对http请求几乎不变的返回是否有缓存
5,java代码优化(日志使用模式填充,数据库查询时能少查字段就少查,复杂处理可以使用线程分离出来)
6,logback日志是否启用异步打印,日志级别很重要,特别是在日志输出多时一定要调整级别为ERROR
7,实在特别复杂的操作抽出来用java线程池(ExecutorService)做

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
 
public class ExtThreadPoolDemo {
    public static class MyTask implements Runnable{
        Subject a;
        public MyTask(Subject ai){
            this.a = ai;
        }
        public void run() {
            System.out.println("Thread Name:" + Thread.currentThread().getName() + "|\t" + a.toString());
  
        }
    }
    public static class Subject{
        private int a;
        private String b;
         
        public Subject(int ai, String bi){
            this.a = ai;
            this.b = bi;
        }
        @Override
        public String toString() {
            return b + "-" + a;
        }
    }
  
    public static void main(String[] args) throws InterruptedException {
        long now = System.currentTimeMillis();
        ExtThreadPoolDemo.MyTask myTask = null;
        ExecutorService es = new ThreadPoolExecutor(5, 5, 0L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(95)){
            @Override
            protected void beforeExecute(Thread t, Runnable r) {
            }
  
            @Override
            protected void afterExecute(Runnable r, Throwable t) {
            }
  
            @Override
            protected void terminated() {
            }
        };
        for (int i = 0; i < 100 ; i++) {
            myTask = new MyTask(new Subject(i,"hs pool^"+i));
            es.execute(myTask);
        }
        es.shutdown();
        long time1 =  (System.currentTimeMillis()  - now);
        System.out.println("time1 :"+time1);
         
         
         
         
//        now = System.currentTimeMillis();
//        for (int i = 0; i < 100 ; i++) {
//          new MyTask(new Subject(i,"no pool ^"+i)).run();
//        }
//        long time2= System.currentTimeMillis()  - now;
//        System.out.println("time2 :"+ time2);
//       
//        System.out.println(time2-time1);
    }
}

spring boot 中使用线程池

复制代码
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import javax.annotation.PreDestroy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;


import lombok.extern.slf4j.Slf4j;
@Slf4j
@Component
public class GeoFenceThreadPool {
    
    public static ThreadPoolExecutor EXECUTOR;
    

    @Autowired
    public void setSpeedServiceImpl(
            @Value("${local.java.pool.corePoolSize:60}")int corePoolSize,
            @Value("${local.java.pool.maximumPoolSize:200}")int maximumPoolSize,
            @Value("${local.java.pool.keepAliveTime:1500}")long keepAliveTime,
            @Value("${local.java.pool.workQueueSize:3000}")int workQueueSize) {
        GeoFenceThreadPool.EXECUTOR = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<Runnable>(workQueueSize));
    }

    
    @PreDestroy
    public void shutdownPool() {
        log.info("GeoFenceThreadPool shutdown");
        EXECUTOR.shutdown();
    }

}
复制代码

常用的linux命令

查看java进程 : jps
监控jvm,每5秒打印一次: jstat -gc 24019 5000
查看进程运行时间:ps -eo pid,tty,user,comm,lstart,etime | grep 24019
返回说明

S0C:第一个幸存区的大小
S1C:第二个幸存区的大小
S0U:第一个幸存区的使用大小
S1U:第二个幸存区的使用大小
EC:伊甸园区的大小
EU:伊甸园区的使用大小
OC:老年代大小
OU:老年代使用大小
MC:方法区大小
MU:方法区使用大小
CCSC:压缩类空间大小
CCSU:压缩类空间使用大小
YGC:年轻代垃圾回收次数
YGCT:年轻代垃圾回收消耗时间
FGC:老年代垃圾回收次数
FGCT:老年代垃圾回收消耗时间
GCT:垃圾回收消耗总时间
posted @   原子切割员  阅读(212)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App
点击右上角即可分享
微信分享提示