-Xms64M -Xmx512M -Xss200K
-Xms64M:设置JVM初始内存为64M。
-Xmx512M:设置JVM最大可用内存512M。
-Xss200K:设置单个线程的大小为200K。
在程序中-Xss设置太小,可能程序会报错。默认设置是512K。但如果需要大量的线程,就可以将-Xss调小来获得更多的线程。
package com.competition.score.test; import java.util.concurrent.CountDownLatch; public class TestThread { public static void main(String[] args) { for (int i = 0;; i++) { System.out.println("i = " + i); new Thread(new HoldThread()).start(); } } } class HoldThread extends Thread { CountDownLatch cdl = new CountDownLatch(1); public HoldThread() { this.setDaemon(true); } public void run() { try { cdl.await(); } catch (InterruptedException e) { } } } |
可用如上程序测试可用的最大线程数。注意:如上程序运行完毕需要重启虚拟机来清除线程占用。