{"action":"created","comment":{"url":"https://api.github.com/repos/akka/akka/pulls/comments/64398384","id":64398384,"diff_hunk":"@@ -54,11 +54,42 @@ private[akka] class DirectByteBufferPool(defaultBufferSize: Int, maxPoolEntries:\n     }\n   }\n \n-  private final def offerBufferToPool(buf: ByteBuffer): Unit =\n-    pool.synchronized {\n-      if (buffersInPool < maxPoolEntries) {\n-        pool(buffersInPool) = buf\n-        buffersInPool += 1\n-      } // else let the buffer be gc'd\n+  private final def offerBufferToPool(buf: ByteBuffer): Unit = {\n+    val clean =\n+      pool.synchronized {\n+        if (buffersInPool < maxPoolEntries) {\n+          pool(buffersInPool) = buf\n+          buffersInPool += 1\n+          false\n+        } else {\n+          // try to clean it outside the lock, or let the buffer be gc'd\n+          true\n+        }\n+      }\n+    if (clean)\n+      tryCleanDirectByteBuffer(buf)\n+  }\n+\n+  /**\n+   * DirectByteBuffers are garbage collected by using a phantom reference and a\n+   * reference queue. Every once a while, the JVM checks the reference queue and\n+   * cleans the DirectByteBuffers. However, as this doesn't happen\n+   * immediately after discarding all references to a DirectByteBuffer, it's\n+   * easy to OutOfMemoryError yourself using DirectByteBuffers. This function\n+   * explicitly calls the Cleaner method of a DirectByteBuffer.\n+   *\n+   * Utilizes reflection to avoid dependency to `sun.misc.Cleaner`.\n+   */\n+  private final def tryCleanDirectByteBuffer(toBeDestroyed: ByteBuffer): Unit = try {\n+    if (toBeDestroyed.isDirect) {\n+      val cleanerMethod = toBeDestroyed.getClass().getMethod(\"cleaner\")\n+      cleanerMethod.setAccessible(true)\n+      val cleaner = cleanerMethod.invoke(toBeDestroyed)\n+      val cleanMethod = cleaner.getClass().getMethod(\"clean\")\n+      cleanMethod.setAccessible(true)\n+      cleanMethod.invoke(cleaner)\n     }\n+  } catch {\n+    case _: Throwable ⇒ // attempt failed, ok","path":"akka-actor/src/main/scala/akka/io/DirectByteBufferPool.scala","position":46,"original_position":46,"commit_id":"83ab46075c1c2bd7da1794c77bfd846477cecd69","original_commit_id":"83ab46075c1c2bd7da1794c77bfd846477cecd69","user":{"login":"ktoso","id":120979,"avatar_url":"https://avatars.githubusercontent.com/u/120979?v=3","gravatar_id":"","url":"https://api.github.com/users/ktoso","html_url":"https://github.com/ktoso","followers_url":"https://api.github.com/users/ktoso/followers","following_url":"https://api.github.com/users/ktoso/following{/other_user}","gists_url":"https://api.github.com/users/ktoso/gists{/gist_id}","starred_url":"https://api.github.com/users/ktoso/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ktoso/subscriptions","organizations_url":"https://api.github.com/users/ktoso/orgs","repos_url":"https://api.github.com/users/ktoso/repos","events_url":"https://api.github.com/users/ktoso/events{/privacy}","received_events_url":"https://api.github.com/users/ktoso/received_events","type":"User","site_admin":false},"body":"Yes, I think so. SecurityException, ClassNotFoundException etc","created_at":"2016-05-24T14:14:38Z","updated_at":"2016-05-24T14:14:38Z","html_url":"https://github.com/akka/akka/pull/20616#discussion_r64398384","pull_request_url":"https://api.github.com/repos/akka/akka/pulls/20616","_links":{"self":{"href":"https://api.github.com/repos/akka/akka/pulls/comments/64398384"},"html":{"href":"https://github.com/akka/akka/pull/20616#discussion_r64398384"},"pull_request":{"href":"https://api.github.com/repos/akka/akka/pulls/20616"}}},"pull_request":{"url":"https://api.github.com/repos/akka/akka/pulls/20616","id":71217138,"html_url":"https://github.com/akka/akka/pull/20616","diff_url":"https://github.com/akka/akka/pull/20616.diff","patch_url":"https://github.com/akka/akka/pull/20616.patch","issue_url":"https://api.github.com/repos/akka/akka/issues/20616","number":20616,"state":"open","locked":false,"title":"Attempt cleaning of DirectByteBuffer, #17194","user":{"login":"patriknw","id":336161,"avatar_url":"https://avatars.githubusercontent.com/u/336161?v=3","gravatar_id":"","url":"https://api.github.com/users/patriknw","html_url":"https://github.com/patriknw","followers_url":"https://api.github.com/users/patriknw/followers","following_url":"https://api.github.com/users/patriknw/following{/other_user}","gists_url":"https://api.github.com/users/patriknw/gists{/gist_id}","starred_url":"https://api.github.com/users/patriknw/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/patriknw/subscriptions","organizations_url":"https://api.github.com/users/patriknw/orgs","repos_url":"https://api.github.com/users/patriknw/repos","events_url":"https://api.github.com/users/patriknw/events{/privacy}","received_events_url":"https://api.github.com/users/patriknw/received_events","type":"User","site_admin":false},"body":"Refs #17194\r\n\r\nSolution from http://stackoverflow.com/questions/1854398/how-to-garbage-collect-a-direct-buffer-java","created_at":"2016-05-24T13:12:48Z","updated_at":"2016-05-24T14:14:38Z","closed_at":null,"merged_at":null,"merge_commit_sha":"a6adf9d03512d9a7d52430e759ea010fff0776b2","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/akka/akka/pulls/20616/commits","review_comments_url":"https://api.github.com/repos/akka/akka/pulls/20616/comments","review_comment_url":"https://api.github.com/repos/akka/akka/pulls/comments{/number}","comments_url":"https://api.github.com/repos/akka/akka/issues/20616/comments","statuses_url":"https://api.github.com/repos/akka/akka/statuses/83ab46075c1c2bd7da1794c77bfd846477cecd69","head":{"label":"akka:wip-17194-DirectByteBuffer-cleaner-patriknw","ref":"wip-17194-DirectByteBuffer-cleaner-patriknw","sha":"83ab46075c1c2bd7da1794c77bfd846477cecd69","user":{"login":"akka","id":1496237,"avatar_url":"https://avatars.githubusercontent.com/u/1496237?v=3","gravatar_id":"","url":"https://api.github.com/users/akka","html_url":"https://github.com/akka","followers_url":"https://api.github.com/users/akka/followers","following_url":"https://api.github.com/users/akka/following{/other_user}","gists_url":"https://api.github.com/users/akka/gists{/gist_id}","starred_url":"https://api.github.com/users/akka/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/akka/subscriptions","organizations_url":"https://api.github.com/users/akka/orgs","repos_url":"https://api.github.com/users/akka/repos","events_url":"https://api.github.com/users/akka/events{/privacy}","received_events_url":"https://api.github.com/users/akka/received_events","type":"Organization","site_admin":false},"repo":{"id":130013,"name":"akka","full_name":"akka/akka","owner":{"login":"akka","id":1496237,"avatar_url":"https://avatars.githubusercontent.com/u/1496237?v=3","gravatar_id":"","url":"https://api.github.com/users/akka","html_url":"https://github.com/akka","followers_url":"https://api.github.com/users/akka/followers","following_url":"https://api.github.com/users/akka/following{/other_user}","gists_url":"https://api.github.com/users/akka/gists{/gist_id}","starred_url":"https://api.github.com/users/akka/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/akka/subscriptions","organizations_url":"https://api.github.com/users/akka/orgs","repos_url":"https://api.github.com/users/akka/repos","events_url":"https://api.github.com/users/akka/events{/privacy}","received_events_url":"https://api.github.com/users/akka/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/akka/akka","description":"Akka Project","fork":false,"url":"https://api.github.com/repos/akka/akka","forks_url":"https://api.github.com/repos/akka/akka/forks","keys_url":"https://api.github.com/repos/akka/akka/keys{/key_id}","collaborators_url":"https://api.github.com/repos/akka/akka/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/akka/akka/teams","hooks_url":"https://api.github.com/repos/akka/akka/hooks","issue_events_url":"https://api.github.com/repos/akka/akka/issues/events{/number}","events_url":"https://api.github.com/repos/akka/akka/events","assignees_url":"https://api.github.com/repos/akka/akka/assignees{/user}","branches_url":"https://api.github.com/repos/akka/akka/branches{/branch}","tags_url":"https://api.github.com/repos/akka/akka/tags","blobs_url":"https://api.github.com/repos/akka/akka/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/akka/akka/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/akka/akka/git/refs{/sha}","trees_url":"https://api.github.com/repos/akka/akka/git/trees{/sha}","statuses_url":"https://api.github.com/repos/akka/akka/statuses/{sha}","languages_url":"https://api.github.com/repos/akka/akka/languages","stargazers_url":"https://api.github.com/repos/akka/akka/stargazers","contributors_url":"https://api.github.com/repos/akka/akka/contributors","subscribers_url":"https://api.github.com/repos/akka/akka/subscribers","subscription_url":"https://api.github.com/repos/akka/akka/subscription","commits_url":"https://api.github.com/repos/akka/akka/commits{/sha}","git_commits_url":"https://api.github.com/repos/akka/akka/git/commits{/sha}","comments_url":"https://api.github.com/repos/akka/akka/comments{/number}","issue_comment_url":"https://api.github.com/repos/akka/akka/issues/comments{/number}","contents_url":"https://api.github.com/repos/akka/akka/contents/{+path}","compare_url":"https://api.github.com/repos/akka/akka/compare/{base}...{head}","merges_url":"https://api.github.com/repos/akka/akka/merges","archive_url":"https://api.github.com/repos/akka/akka/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/akka/akka/downloads","issues_url":"https://api.github.com/repos/akka/akka/issues{/number}","pulls_url":"https://api.github.com/repos/akka/akka/pulls{/number}","milestones_url":"https://api.github.com/repos/akka/akka/milestones{/number}","notifications_url":"https://api.github.com/repos/akka/akka/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/akka/akka/labels{/name}","releases_url":"https://api.github.com/repos/akka/akka/releases{/id}","deployments_url":"https://api.github.com/repos/akka/akka/deployments","created_at":"2009-02-16T12:51:54Z","updated_at":"2016-05-24T11:25:32Z","pushed_at":"2016-05-24T14:11:13Z","git_url":"git://github.com/akka/akka.git","ssh_url":"git@github.com:akka/akka.git","clone_url":"https://github.com/akka/akka.git","svn_url":"https://github.com/akka/akka","homepage":"http://akka.io","size":231379,"stargazers_count":5547,"watchers_count":5547,"language":"Scala","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":1723,"mirror_url":null,"open_issues_count":689,"forks":1723,"open_issues":689,"watchers":5547,"default_branch":"master"}},"base":{"label":"akka:master","ref":"master","sha":"31de5952b09aedba5887caf52ef38fd9ef84fc5f","user":{"login":"akka","id":1496237,"avatar_url":"https://avatars.githubusercontent.com/u/1496237?v=3","gravatar_id":"","url":"https://api.github.com/users/akka","html_url":"https://github.com/akka","followers_url":"https://api.github.com/users/akka/followers","following_url":"https://api.github.com/users/akka/following{/other_user}","gists_url":"https://api.github.com/users/akka/gists{/gist_id}","starred_url":"https://api.github.com/users/akka/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/akka/subscriptions","organizations_url":"https://api.github.com/users/akka/orgs","repos_url":"https://api.github.com/users/akka/repos","events_url":"https://api.github.com/users/akka/events{/privacy}","received_events_url":"https://api.github.com/users/akka/received_events","type":"Organization","site_admin":false},"repo":{"id":130013,"name":"akka","full_name":"akka/akka","owner":{"login":"akka","id":1496237,"avatar_url":"https://avatars.githubusercontent.com/u/1496237?v=3","gravatar_id":"","url":"https://api.github.com/users/akka","html_url":"https://github.com/akka","followers_url":"https://api.github.com/users/akka/followers","following_url":"https://api.github.com/users/akka/following{/other_user}","gists_url":"https://api.github.com/users/akka/gists{/gist_id}","starred_url":"https://api.github.com/users/akka/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/akka/subscriptions","organizations_url":"https://api.github.com/users/akka/orgs","repos_url":"https://api.github.com/users/akka/repos","events_url":"https://api.github.com/users/akka/events{/privacy}","received_events_url":"https://api.github.com/users/akka/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/akka/akka","description":"Akka Project","fork":false,"url":"https://api.github.com/repos/akka/akka","forks_url":"https://api.github.com/repos/akka/akka/forks","keys_url":"https://api.github.com/repos/akka/akka/keys{/key_id}","collaborators_url":"https://api.github.com/repos/akka/akka/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/akka/akka/teams","hooks_url":"https://api.github.com/repos/akka/akka/hooks","issue_events_url":"https://api.github.com/repos/akka/akka/issues/events{/number}","events_url":"https://api.github.com/repos/akka/akka/events","assignees_url":"https://api.github.com/repos/akka/akka/assignees{/user}","branches_url":"https://api.github.com/repos/akka/akka/branches{/branch}","tags_url":"https://api.github.com/repos/akka/akka/tags","blobs_url":"https://api.github.com/repos/akka/akka/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/akka/akka/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/akka/akka/git/refs{/sha}","trees_url":"https://api.github.com/repos/akka/akka/git/trees{/sha}","statuses_url":"https://api.github.com/repos/akka/akka/statuses/{sha}","languages_url":"https://api.github.com/repos/akka/akka/languages","stargazers_url":"https://api.github.com/repos/akka/akka/stargazers","contributors_url":"https://api.github.com/repos/akka/akka/contributors","subscribers_url":"https://api.github.com/repos/akka/akka/subscribers","subscription_url":"https://api.github.com/repos/akka/akka/subscription","commits_url":"https://api.github.com/repos/akka/akka/commits{/sha}","git_commits_url":"https://api.github.com/repos/akka/akka/git/commits{/sha}","comments_url":"https://api.github.com/repos/akka/akka/comments{/number}","issue_comment_url":"https://api.github.com/repos/akka/akka/issues/comments{/number}","contents_url":"https://api.github.com/repos/akka/akka/contents/{+path}","compare_url":"https://api.github.com/repos/akka/akka/compare/{base}...{head}","merges_url":"https://api.github.com/repos/akka/akka/merges","archive_url":"https://api.github.com/repos/akka/akka/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/akka/akka/downloads","issues_url":"https://api.github.com/repos/akka/akka/issues{/number}","pulls_url":"https://api.github.com/repos/akka/akka/pulls{/number}","milestones_url":"https://api.github.com/repos/akka/akka/milestones{/number}","notifications_url":"https://api.github.com/repos/akka/akka/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/akka/akka/labels{/name}","releases_url":"https://api.github.com/repos/akka/akka/releases{/id}","deployments_url":"https://api.github.com/repos/akka/akka/deployments","created_at":"2009-02-16T12:51:54Z","updated_at":"2016-05-24T11:25:32Z","pushed_at":"2016-05-24T14:11:13Z","git_url":"git://github.com/akka/akka.git","ssh_url":"git@github.com:akka/akka.git","clone_url":"https://github.com/akka/akka.git","svn_url":"https://github.com/akka/akka","homepage":"http://akka.io","size":231379,"stargazers_count":5547,"watchers_count":5547,"language":"Scala","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":1723,"mirror_url":null,"open_issues_count":689,"forks":1723,"open_issues":689,"watchers":5547,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/akka/akka/pulls/20616"},"html":{"href":"https://github.com/akka/akka/pull/20616"},"issue":{"href":"https://api.github.com/repos/akka/akka/issues/20616"},"comments":{"href":"https://api.github.com/repos/akka/akka/issues/20616/comments"},"review_comments":{"href":"https://api.github.com/repos/akka/akka/pulls/20616/comments"},"review_comment":{"href":"https://api.github.com/repos/akka/akka/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/akka/akka/pulls/20616/commits"},"statuses":{"href":"https://api.github.com/repos/akka/akka/statuses/83ab46075c1c2bd7da1794c77bfd846477cecd69"}}}}
{"actor":{"display_login":"ktoso"}}