ExecutorService java.lang.OutOfMemoryError: unable to create new native thread



  • Using ExecutorService for a multi-point server. In the Eklips launch, an exception is granted to java.lang.OutOfMemoryError: unable to create new native thread Everything works fine without ExecutorService.

    final ServerSocket welcomeSocket = new ServerSocket(1237);
            final ExecutorService service = Executors.newCachedThreadPool();
            while (!Thread.currentThread().isInterrupted()) 
            {
    
            service.submit(new Runnable() {
                public void run() {
                    while (true) {try{
                    // code
    

    }catch(Exception e){try {welcomeSocket.close();service.shutdown(); } catch (IOException e1) {e1.printStackTrace();}}
    }}});}

    What's wrong with my code?



  • No wonder. You're in an endless cycle doing endless service assignments.

    What does the service do when they get a new assignment? http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Executors.html#newCachedThreadPool%28%29 to you. service:

    Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available. [...] Calls to execute will reuse previously constructed threads if available. If no existing thread is available, a new thread will be created and added to the pool.

    I do.

    Creates a pool of fluxes that creates new flows where necessary, but uses the old ones if they are available. [...] Challenges execute will use previously created flows if they are free. If not, a new flow will be created and added to the pool.

    I mean, since your assignments don't end, there's no reuse of flows, and you're in the cycle setting up one at a time.

    No wonder they end sooner or later.

    You need to review your code architecture. Such a form would not function in principle.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2