{"action":"closed","issue":{"url":"https://api.github.com/repos/puniverse/quasar/issues/74","labels_url":"https://api.github.com/repos/puniverse/quasar/issues/74/labels{/name}","comments_url":"https://api.github.com/repos/puniverse/quasar/issues/74/comments","events_url":"https://api.github.com/repos/puniverse/quasar/issues/74/events","html_url":"https://github.com/puniverse/quasar/issues/74","id":54184079,"number":74,"title":"Repeatedly calling FiberSocketChannel.open() results in java.lang.OutOfMemoryError: unable to create new native thread","user":{"login":"mnowrot","id":135707,"avatar_url":"https://avatars.githubusercontent.com/u/135707?v=3","gravatar_id":"","url":"https://api.github.com/users/mnowrot","html_url":"https://github.com/mnowrot","followers_url":"https://api.github.com/users/mnowrot/followers","following_url":"https://api.github.com/users/mnowrot/following{/other_user}","gists_url":"https://api.github.com/users/mnowrot/gists{/gist_id}","starred_url":"https://api.github.com/users/mnowrot/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mnowrot/subscriptions","organizations_url":"https://api.github.com/users/mnowrot/orgs","repos_url":"https://api.github.com/users/mnowrot/repos","events_url":"https://api.github.com/users/mnowrot/events{/privacy}","received_events_url":"https://api.github.com/users/mnowrot/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/puniverse/quasar/labels/bug","name":"bug","color":"fc2929"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":4,"created_at":"2015-01-13T12:01:41Z","updated_at":"2015-01-14T11:59:50Z","closed_at":"2015-01-14T11:59:50Z","body":"Having the following inside a fiber's run method\r\n\r\n```\r\nfor(int i = 0; i < runsCount; i++) {\r\n    // connect\r\n    FiberSocketChannel channel = FiberSocketChannel.open();\r\n    (...)\r\n    channel.close();\r\n}\r\n```\r\nresults in \"OOMError unable to create new native thread\" thrown from open() when the runsCount is big enough.\r\nAfter investigation, this seems to be caused by repeatedly calling FiberAsyncIO.defaultGroup();\r\n\r\nThere is a workaround:\r\nInitialize the AsynchronousChannelGroup once per Fiber class:\r\n```\r\nprivate static final AsynchronousChannelGroup group;\r\nstatic {\r\n   try {\r\n      group = AsynchronousChannelGroup.withThreadPool(Executors.newFixedThreadPool(4));\r\n   } catch (IOException e) {\r\n      throw new RuntimeException(\"Failed thread creation\", e)\r\n   }\r\n}\r\n```\r\nand in the code above replace\r\n```\r\nFiberSocketChannel.open();\r\n```\r\nwith\r\n```\r\nFiberSocketChannel.open(group);\r\n```\r\n"}}