LINUX.ORG.RU

java 1000 тредов. Съедает память

 ,


1

1

Взял тестовый сорс из книги, немного его подрихтовал на запуск 1000 тредов. Запустил, меряю ps_mem -p pid и вижу, что потихонечку память процесса увеличивается. Это нормально или где-то тут протекает? :)

// Create a second thread.
class NewThread implements Runnable {
  Thread t;

  NewThread(String name) {
    // Create a new, second thread
    t = new Thread(this, name);
    System.out.println("Child thread: " + t);
  }

  // This is the entry point for the second thread.
  public void run() {
    try {
      // for (int i = 5; i > 0; i--) {
      for (int i = 5;; i--) {
        System.out.println(Thread.currentThread());
        System.out.println("Child Thread: " + i);
        Thread.sleep(500);
      }
    } catch (InterruptedException e) {
      System.out.println("Child interrupted.");
    }
    System.out.println("Exiting child thread.");
  }
}

class HelloWorld {
  public static void main(String[] args) {
    // NewThread nt = new NewThread(); // create a new thread
    // nt.t.start(); // Start the thread

    for(int x = 0; x <= 1000; x++) {
      System.out.println("aaaa");
      NewThread nt = new NewThread("some" + x); // create a new thread
      nt.t.start(); // Start the thread
    }

    try {
      for (int i = 5;; i--) {
        System.out.println("Main Thread: " + i);
        Thread.sleep(1000);
      }
    } catch (InterruptedException e) {
      System.out.println("Main thread interrupted.");
    }
    System.out.println("Main thread exiting.");
  }
}
★★★★

запустите jconsole, подключитесь к вашей проге и поглядите вживую что происходит. Там кстати видно будет как сборщик мусора отрабатывается. И принудительно запустить его можно и посмотреть что произойдет.

saibogo ★★★★
()
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.