java 进程中的实际线程数是多少?
What is actual number of threads in a java process?
背景
- 在下面的代码中,"a" 线程被创建并启动。
- 运行 方法包含无限循环。
- 在循环中,成员变量"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 个线程 运行ning。
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
(由于 space 约束只显示了少数行)
问题是在程序最初 运行 运行大约 2 个月后观察到的,盒子上的线程数约为 70(使用 ps 命令)和 "top" 显示 VIRT 内存使用量为 12 GB。
重新启动程序后,第23个线程被添加到上面的列表中
一天(24 小时)后的线程,增加虚拟内存。那么问题就来了,需要弄清楚为什么?
问题
程序创建了多少个线程?
是什么导致 ps 命令显示这么多 "threads"?
为什么线程数会随着时间的推移而增加,从而导致内存使用量增加?
问题是对使用的库 类 的处理不当。我们使用 kafka.javaapi.producer.Producer
并且在刷新配置 json 文件之前没有调用方法 close()
。这导致了非常意外的行为。我们还删除了线程部分,因为 main
线程足以执行任务,而不是陷入并发开销。
背景
- 在下面的代码中,"a" 线程被创建并启动。
- 运行 方法包含无限循环。
- 在循环中,成员变量"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 个线程 运行ning。
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
(由于 space 约束只显示了少数行)
问题是在程序最初 运行 运行大约 2 个月后观察到的,盒子上的线程数约为 70(使用 ps 命令)和 "top" 显示 VIRT 内存使用量为 12 GB。
重新启动程序后,第23个线程被添加到上面的列表中 一天(24 小时)后的线程,增加虚拟内存。那么问题就来了,需要弄清楚为什么?
问题
程序创建了多少个线程?
是什么导致 ps 命令显示这么多 "threads"?
为什么线程数会随着时间的推移而增加,从而导致内存使用量增加?
问题是对使用的库 类 的处理不当。我们使用 kafka.javaapi.producer.Producer
并且在刷新配置 json 文件之前没有调用方法 close()
。这导致了非常意外的行为。我们还删除了线程部分,因为 main
线程足以执行任务,而不是陷入并发开销。