java - java进程中的实际线程数是多少?

标签 java multithreading memory memory-leaks

背景

  • 在下面的代码中,创建并启动了“一个”线程。
  • run 方法包含无限循环。
  • 在循环中,每隔一定时间间隔,成员变量“prod”就会被重新分配一个新配置的对象。
  • 配置基于 json 对象,而该对象又使用配置文件创建

    public class Producer extends Thread {
        private long lastReadTime; 
        private long refreshInterval; 
        private String configFile; 
        private JSONObject configJson;
        private MyProducer prod; 
    
        public StdInProducer (String filename) throws IOException { 
           this.configFile = filename; 
           this.refreshConfig(); 
        } 
    
        public void refreshConfig() { 
            this.lastReadTime = System.currentTimeMillis(); 
            this.configJson = new JSONObject(FileUtils.readFileToString(new File(this.configFile), "UTF-8"));
            this.refreshInterval = this.confJsonObj.optLong("refreshInterval", 86400);
            this.initializeProducer(configJson); 
        }
    
        private void initializeProducer(JSONObject confJsonObj) {
            //initialise producer using json object values
            this.prod = // new MyProducer obj with settings from json obj
        }
    
        public void run() {
             while(true) {
                long currentTime = System.currentTimeMillis();
                if(currentTime - this.lastReadTime > this.refreshInterval*1000) {
                    this.refreshConfig();    
                }
               // Rest of the code 
            }
       }
     }
    
    public static void main(String[] args) {
        String configFilename = args[0]; 
        Thread t = new Producer(configFilename);
        t.start();
    }
    

观察

Thread.activeCount()

显示输出为 2

ps -aefL | grep producer | grep -v "grep" | wc -l

显示程序启动时最初运行的 22 个线程。

ps -aefL | grep producer 
root     18498     1 18498  0   22 Jul22 ?        00:00:00 /usr/bin/java -cp producer-1.0.jar stdin.producer.Producer stdinConfig.json                                                                     
root     18498     1 18499  0   22 Jul22 ?        00:00:00 /usr/bin/java -cp producer-1.0.jar stdin.producer.Producer stdinConfig.json 
root     18498     1 18500  0   22 Jul22 ?        00:01:55 /usr/bin/java -cp producer-1.0.jar stdin.producer.Producer stdinConfig.json 
root     18498     1 18501  0   22 Jul22 ?        00:01:55 /usr/bin/java -cp producer-1.0.jar stdin.producer.Producer stdinConfig.json 

(由于空间限制,仅显示几行)

在程序首次运行大约 2 个月后,观察到一个盒子上的线程数约为 70(使用 ps 命令),并且“top”显示 VIRT mem 使用量为 12 GB,从而发现了该问题。

重新启动程序后,第 23 个线程被添加到上面的列表中 一天(24 小时)后的线程,增加虚拟内存。那么问题就在那里,需要找出原因吗?

问题

程序创建了多少个线程?

是什么导致 ps 命令显示如此多的“线程”?

为什么线程数量随着时间的推移而增加,内存使用量也随之增加?

最佳答案

问题是对所使用的库类的处理不当。我们使用了 kafka.javaapi. Producer.Producer ,并且在刷新配置 json 文件之前没有调用 close() 方法。这导致了非常不可预测的行为。我们还删除了线程部分,因为只需 main 线程就足以执行任务,而不会产生并发开销。

关于java - java进程中的实际线程数是多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31575764/

相关文章:

java - 类名是表达式吗?

JAVA 在多线程中使用不同的paint()方法

java - 如何捕获 Runnable 中的封闭范围

c++ - CodeBlocks c++ - 不能使用线程,因为编译器不支持它

php - Doctrine ORM 内存问题

java - ClassNotFoundException : com. google.appengine.tools.development.DevAppServerMain

Java进程处理: keep correct order with output+error stream?

Python,内存错误,csv文件太大

java - override onCreate 和在子类中无所事事之间的区别?

memory - 跨线程协作内存使用?