{"action":"created","comment":{"url":"https://api.github.com/repos/realm/realm-java/pulls/comments/89099298","pull_request_review_id":9648090,"id":89099298,"diff_hunk":"@@ -43,11 +43,16 @@\n     private static final long PRIMARY_KEY_FIELD_COLUMN_INDEX = 1;\n     private static final long NO_PRIMARY_KEY = -2;\n \n-    long nativePtr;\n+    protected long nativePtr;\n+    private static final long nativeFinalizerPtr;\n     private final Context context;\n     private final SharedRealm sharedRealm;\n     private long cachedPrimaryKeyColumnIndex = NO_MATCH;\n \n+    static {","path":"realm/realm-library/src/main/java/io/realm/internal/Table.java","position":20,"original_position":20,"commit_id":"25264b73291a35d223810c80ab36c857abe695f9","original_commit_id":"25264b73291a35d223810c80ab36c857abe695f9","user":{"login":"beeender","id":449296,"avatar_url":"https://avatars.githubusercontent.com/u/449296?v=3","gravatar_id":"","url":"https://api.github.com/users/beeender","html_url":"https://github.com/beeender","followers_url":"https://api.github.com/users/beeender/followers","following_url":"https://api.github.com/users/beeender/following{/other_user}","gists_url":"https://api.github.com/users/beeender/gists{/gist_id}","starred_url":"https://api.github.com/users/beeender/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/beeender/subscriptions","organizations_url":"https://api.github.com/users/beeender/orgs","repos_url":"https://api.github.com/users/beeender/repos","events_url":"https://api.github.com/users/beeender/events{/privacy}","received_events_url":"https://api.github.com/users/beeender/received_events","type":"User","site_admin":false},"body":"There might be a reason but I cannot remember ... maybe my brain was shorted at that time ... I will change it to setting directly.","created_at":"2016-11-22T11:51:34Z","updated_at":"2016-11-22T11:51:34Z","html_url":"https://github.com/realm/realm-java/pull/3144#discussion_r89099298","pull_request_url":"https://api.github.com/repos/realm/realm-java/pulls/3144","_links":{"self":{"href":"https://api.github.com/repos/realm/realm-java/pulls/comments/89099298"},"html":{"href":"https://github.com/realm/realm-java/pull/3144#discussion_r89099298"},"pull_request":{"href":"https://api.github.com/repos/realm/realm-java/pulls/3144"}}},"pull_request":{"url":"https://api.github.com/repos/realm/realm-java/pulls/3144","id":76380589,"html_url":"https://github.com/realm/realm-java/pull/3144","diff_url":"https://github.com/realm/realm-java/pull/3144.diff","patch_url":"https://github.com/realm/realm-java/pull/3144.patch","issue_url":"https://api.github.com/repos/realm/realm-java/issues/3144","number":3144,"state":"open","locked":false,"title":"BYE BYE finalizer","user":{"login":"beeender","id":449296,"avatar_url":"https://avatars.githubusercontent.com/u/449296?v=3","gravatar_id":"","url":"https://api.github.com/users/beeender","html_url":"https://github.com/beeender","followers_url":"https://api.github.com/users/beeender/followers","following_url":"https://api.github.com/users/beeender/following{/other_user}","gists_url":"https://api.github.com/users/beeender/gists{/gist_id}","starred_url":"https://api.github.com/users/beeender/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/beeender/subscriptions","organizations_url":"https://api.github.com/users/beeender/orgs","repos_url":"https://api.github.com/users/beeender/repos","events_url":"https://api.github.com/users/beeender/events{/privacy}","received_events_url":"https://api.github.com/users/beeender/received_events","type":"User","site_admin":false},"body":"This PR is trying to move the native desctructions from finalizer to Phantom Deamon.\r\nAnd some ideas are inspired by https://android-review.googlesource.com/#/c/175765/10\r\n- [x] Use linked list for phantom reference queue which should be faster than current array implementation. (no more chunk allocation)\r\n- [x] Use function pointer to generalize the deallocation for native objects instead of current `switch`/`case` based routine.\r\n- [x] All native objects go with the phantom reference, (maybe even the `SharedGroup`/`Group`, needs to confirm). _This will be handled in other PR._\r\n- [x] Use Phantom Deamon thread to do deallocation.\r\n- [x] Check if we still need lock on `Context` if we can do deallocation only on one thread.\r\n- [x] Some exciting benchmarks.\r\n\r\n#\r\n## Why?\r\n* To close #2496 . Some UI freeze is caused by the Realm's finalizer.\r\n* See [Finalization and Phantom References](https://dzone.com/articles/finalization-and-phantom) about the problem with with Finalization and why the phantom reference is preferred.\r\n* It basically simplify our code for deallocating native resource.\r\n\r\n# \r\n## Memory Usage Benchmarks (Mem usage in KB)\r\n\r\nThe memory usage benchmark is done by [this script](https://gist.github.com/beeender/730b058e3e44985444e4a8402bfb7335). It gets the java/native alloc heap every 100ms.\r\n\r\n**From the results, it shows a slightly better memory usage for low end devices, but more memory used for high end devices. It doesn't necessarily mean that Phantom reference it better/worse than finalizer, but it tells we don't have a native memory pressure problem with it.**\r\n\r\nBlow benchmark measure and compare the memory usage during running realm-java benchmark unit tests. \r\n\r\n| Device | Average Java Heap | Peak Java Heap | Average Native Heap | Peak Native Heap |\r\n| --- | --- | --- | --- | --- |\r\n| Nokia X2 4.3 512MB - Phantom | 7116 | 9328 | 2983 | 24409 |\r\n| Nokia X2 4.3 512MB - Finalizer | 7198 | 9423 | 3104 | 24408 |\r\n| Simulator 6.0 1.5GB - Phantom | 4174 | 23191 | 51624 | 271318 |\r\n| Simulator 6.0 1.5GB - Finalizer | 3833 | 17921 | 35738 | 225334 |\r\n| Huawei Honor 7 6.0 3GB - Phantom | 18409 | 24979 | 50426 | 101464 |\r\n| Huawei Honor 7 6.0 3GB - Finalizer | 18436 | 25172 | 42681 | 86378 |\r\n| Xoom 4.1.2 1GB - Phantom | 3155 | 6658 | 2373 | 14691 |\r\n| Xoom 4.1.2 1GB - Finalizer | 3207 | 5708 | 4349 | 21698 |\r\n## Performance Benchmarks\r\n\r\nThe left results are for Phantom deamon, the right ones are for finalizers. Tests are done with Xoom 4.1.2 device. No big changes in most cases except objects creations.\r\n\r\n![phantomvsfinalizer](https://cloud.githubusercontent.com/assets/449296/16872799/dc2531ec-4ac1-11e6-90db-876023d16bcb.png)\r\n# \r\n## Commit Message\r\n\r\nUse phantom reference and daemon thread to replace finalizer for\r\ndestruct native objects.\r\n\r\nSome key points of this commit:\r\n- The phantom reference pool is implemented as a linked list which\r\n  suppose to be fast insertion/removal.\r\n- A daemon thread is created to monitor and free all phantom reference.\r\n- Delayed disposal before native object creation is not needed any more.\r\n- SharedGroup still gets freed in the caller thread with a lock on\r\n  context.\r\n- Native object needs to pass a destruction function pointer in addition\r\n  to the native object pointer.\r\n","created_at":"2016-07-06T05:30:57Z","updated_at":"2016-11-22T11:51:34Z","closed_at":null,"merged_at":null,"merge_commit_sha":"e7c71fdae421ae60af65df92e855e39076e32677","assignee":{"login":"beeender","id":449296,"avatar_url":"https://avatars.githubusercontent.com/u/449296?v=3","gravatar_id":"","url":"https://api.github.com/users/beeender","html_url":"https://github.com/beeender","followers_url":"https://api.github.com/users/beeender/followers","following_url":"https://api.github.com/users/beeender/following{/other_user}","gists_url":"https://api.github.com/users/beeender/gists{/gist_id}","starred_url":"https://api.github.com/users/beeender/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/beeender/subscriptions","organizations_url":"https://api.github.com/users/beeender/orgs","repos_url":"https://api.github.com/users/beeender/repos","events_url":"https://api.github.com/users/beeender/events{/privacy}","received_events_url":"https://api.github.com/users/beeender/received_events","type":"User","site_admin":false},"assignees":[{"login":"beeender","id":449296,"avatar_url":"https://avatars.githubusercontent.com/u/449296?v=3","gravatar_id":"","url":"https://api.github.com/users/beeender","html_url":"https://github.com/beeender","followers_url":"https://api.github.com/users/beeender/followers","following_url":"https://api.github.com/users/beeender/following{/other_user}","gists_url":"https://api.github.com/users/beeender/gists{/gist_id}","starred_url":"https://api.github.com/users/beeender/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/beeender/subscriptions","organizations_url":"https://api.github.com/users/beeender/orgs","repos_url":"https://api.github.com/users/beeender/repos","events_url":"https://api.github.com/users/beeender/events{/privacy}","received_events_url":"https://api.github.com/users/beeender/received_events","type":"User","site_admin":false}],"milestone":null,"commits_url":"https://api.github.com/repos/realm/realm-java/pulls/3144/commits","review_comments_url":"https://api.github.com/repos/realm/realm-java/pulls/3144/comments","review_comment_url":"https://api.github.com/repos/realm/realm-java/pulls/comments{/number}","comments_url":"https://api.github.com/repos/realm/realm-java/issues/3144/comments","statuses_url":"https://api.github.com/repos/realm/realm-java/statuses/25264b73291a35d223810c80ab36c857abe695f9","head":{"label":"realm:mc/bye-bye-finalizer","ref":"mc/bye-bye-finalizer","sha":"25264b73291a35d223810c80ab36c857abe695f9","user":{"login":"realm","id":7575099,"avatar_url":"https://avatars.githubusercontent.com/u/7575099?v=3","gravatar_id":"","url":"https://api.github.com/users/realm","html_url":"https://github.com/realm","followers_url":"https://api.github.com/users/realm/followers","following_url":"https://api.github.com/users/realm/following{/other_user}","gists_url":"https://api.github.com/users/realm/gists{/gist_id}","starred_url":"https://api.github.com/users/realm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/realm/subscriptions","organizations_url":"https://api.github.com/users/realm/orgs","repos_url":"https://api.github.com/users/realm/repos","events_url":"https://api.github.com/users/realm/events{/privacy}","received_events_url":"https://api.github.com/users/realm/received_events","type":"Organization","site_admin":false},"repo":{"id":4084908,"name":"realm-java","full_name":"realm/realm-java","owner":{"login":"realm","id":7575099,"avatar_url":"https://avatars.githubusercontent.com/u/7575099?v=3","gravatar_id":"","url":"https://api.github.com/users/realm","html_url":"https://github.com/realm","followers_url":"https://api.github.com/users/realm/followers","following_url":"https://api.github.com/users/realm/following{/other_user}","gists_url":"https://api.github.com/users/realm/gists{/gist_id}","starred_url":"https://api.github.com/users/realm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/realm/subscriptions","organizations_url":"https://api.github.com/users/realm/orgs","repos_url":"https://api.github.com/users/realm/repos","events_url":"https://api.github.com/users/realm/events{/privacy}","received_events_url":"https://api.github.com/users/realm/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/realm/realm-java","description":"Realm is a mobile database: a replacement for SQLite & ORMs","fork":false,"url":"https://api.github.com/repos/realm/realm-java","forks_url":"https://api.github.com/repos/realm/realm-java/forks","keys_url":"https://api.github.com/repos/realm/realm-java/keys{/key_id}","collaborators_url":"https://api.github.com/repos/realm/realm-java/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/realm/realm-java/teams","hooks_url":"https://api.github.com/repos/realm/realm-java/hooks","issue_events_url":"https://api.github.com/repos/realm/realm-java/issues/events{/number}","events_url":"https://api.github.com/repos/realm/realm-java/events","assignees_url":"https://api.github.com/repos/realm/realm-java/assignees{/user}","branches_url":"https://api.github.com/repos/realm/realm-java/branches{/branch}","tags_url":"https://api.github.com/repos/realm/realm-java/tags","blobs_url":"https://api.github.com/repos/realm/realm-java/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/realm/realm-java/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/realm/realm-java/git/refs{/sha}","trees_url":"https://api.github.com/repos/realm/realm-java/git/trees{/sha}","statuses_url":"https://api.github.com/repos/realm/realm-java/statuses/{sha}","languages_url":"https://api.github.com/repos/realm/realm-java/languages","stargazers_url":"https://api.github.com/repos/realm/realm-java/stargazers","contributors_url":"https://api.github.com/repos/realm/realm-java/contributors","subscribers_url":"https://api.github.com/repos/realm/realm-java/subscribers","subscription_url":"https://api.github.com/repos/realm/realm-java/subscription","commits_url":"https://api.github.com/repos/realm/realm-java/commits{/sha}","git_commits_url":"https://api.github.com/repos/realm/realm-java/git/commits{/sha}","comments_url":"https://api.github.com/repos/realm/realm-java/comments{/number}","issue_comment_url":"https://api.github.com/repos/realm/realm-java/issues/comments{/number}","contents_url":"https://api.github.com/repos/realm/realm-java/contents/{+path}","compare_url":"https://api.github.com/repos/realm/realm-java/compare/{base}...{head}","merges_url":"https://api.github.com/repos/realm/realm-java/merges","archive_url":"https://api.github.com/repos/realm/realm-java/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/realm/realm-java/downloads","issues_url":"https://api.github.com/repos/realm/realm-java/issues{/number}","pulls_url":"https://api.github.com/repos/realm/realm-java/pulls{/number}","milestones_url":"https://api.github.com/repos/realm/realm-java/milestones{/number}","notifications_url":"https://api.github.com/repos/realm/realm-java/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/realm/realm-java/labels{/name}","releases_url":"https://api.github.com/repos/realm/realm-java/releases{/id}","deployments_url":"https://api.github.com/repos/realm/realm-java/deployments","created_at":"2012-04-20T09:18:42Z","updated_at":"2016-11-22T10:13:53Z","pushed_at":"2016-11-22T08:20:42Z","git_url":"git://github.com/realm/realm-java.git","ssh_url":"git@github.com:realm/realm-java.git","clone_url":"https://github.com/realm/realm-java.git","svn_url":"https://github.com/realm/realm-java","homepage":"http://realm.io","size":70211,"stargazers_count":6506,"watchers_count":6506,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":994,"mirror_url":null,"open_issues_count":336,"forks":994,"open_issues":336,"watchers":6506,"default_branch":"master"}},"base":{"label":"realm:releases","ref":"releases","sha":"d837a98eacc1a48720e0c57e8659fcc3dcea5797","user":{"login":"realm","id":7575099,"avatar_url":"https://avatars.githubusercontent.com/u/7575099?v=3","gravatar_id":"","url":"https://api.github.com/users/realm","html_url":"https://github.com/realm","followers_url":"https://api.github.com/users/realm/followers","following_url":"https://api.github.com/users/realm/following{/other_user}","gists_url":"https://api.github.com/users/realm/gists{/gist_id}","starred_url":"https://api.github.com/users/realm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/realm/subscriptions","organizations_url":"https://api.github.com/users/realm/orgs","repos_url":"https://api.github.com/users/realm/repos","events_url":"https://api.github.com/users/realm/events{/privacy}","received_events_url":"https://api.github.com/users/realm/received_events","type":"Organization","site_admin":false},"repo":{"id":4084908,"name":"realm-java","full_name":"realm/realm-java","owner":{"login":"realm","id":7575099,"avatar_url":"https://avatars.githubusercontent.com/u/7575099?v=3","gravatar_id":"","url":"https://api.github.com/users/realm","html_url":"https://github.com/realm","followers_url":"https://api.github.com/users/realm/followers","following_url":"https://api.github.com/users/realm/following{/other_user}","gists_url":"https://api.github.com/users/realm/gists{/gist_id}","starred_url":"https://api.github.com/users/realm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/realm/subscriptions","organizations_url":"https://api.github.com/users/realm/orgs","repos_url":"https://api.github.com/users/realm/repos","events_url":"https://api.github.com/users/realm/events{/privacy}","received_events_url":"https://api.github.com/users/realm/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/realm/realm-java","description":"Realm is a mobile database: a replacement for SQLite & ORMs","fork":false,"url":"https://api.github.com/repos/realm/realm-java","forks_url":"https://api.github.com/repos/realm/realm-java/forks","keys_url":"https://api.github.com/repos/realm/realm-java/keys{/key_id}","collaborators_url":"https://api.github.com/repos/realm/realm-java/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/realm/realm-java/teams","hooks_url":"https://api.github.com/repos/realm/realm-java/hooks","issue_events_url":"https://api.github.com/repos/realm/realm-java/issues/events{/number}","events_url":"https://api.github.com/repos/realm/realm-java/events","assignees_url":"https://api.github.com/repos/realm/realm-java/assignees{/user}","branches_url":"https://api.github.com/repos/realm/realm-java/branches{/branch}","tags_url":"https://api.github.com/repos/realm/realm-java/tags","blobs_url":"https://api.github.com/repos/realm/realm-java/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/realm/realm-java/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/realm/realm-java/git/refs{/sha}","trees_url":"https://api.github.com/repos/realm/realm-java/git/trees{/sha}","statuses_url":"https://api.github.com/repos/realm/realm-java/statuses/{sha}","languages_url":"https://api.github.com/repos/realm/realm-java/languages","stargazers_url":"https://api.github.com/repos/realm/realm-java/stargazers","contributors_url":"https://api.github.com/repos/realm/realm-java/contributors","subscribers_url":"https://api.github.com/repos/realm/realm-java/subscribers","subscription_url":"https://api.github.com/repos/realm/realm-java/subscription","commits_url":"https://api.github.com/repos/realm/realm-java/commits{/sha}","git_commits_url":"https://api.github.com/repos/realm/realm-java/git/commits{/sha}","comments_url":"https://api.github.com/repos/realm/realm-java/comments{/number}","issue_comment_url":"https://api.github.com/repos/realm/realm-java/issues/comments{/number}","contents_url":"https://api.github.com/repos/realm/realm-java/contents/{+path}","compare_url":"https://api.github.com/repos/realm/realm-java/compare/{base}...{head}","merges_url":"https://api.github.com/repos/realm/realm-java/merges","archive_url":"https://api.github.com/repos/realm/realm-java/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/realm/realm-java/downloads","issues_url":"https://api.github.com/repos/realm/realm-java/issues{/number}","pulls_url":"https://api.github.com/repos/realm/realm-java/pulls{/number}","milestones_url":"https://api.github.com/repos/realm/realm-java/milestones{/number}","notifications_url":"https://api.github.com/repos/realm/realm-java/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/realm/realm-java/labels{/name}","releases_url":"https://api.github.com/repos/realm/realm-java/releases{/id}","deployments_url":"https://api.github.com/repos/realm/realm-java/deployments","created_at":"2012-04-20T09:18:42Z","updated_at":"2016-11-22T10:13:53Z","pushed_at":"2016-11-22T08:20:42Z","git_url":"git://github.com/realm/realm-java.git","ssh_url":"git@github.com:realm/realm-java.git","clone_url":"https://github.com/realm/realm-java.git","svn_url":"https://github.com/realm/realm-java","homepage":"http://realm.io","size":70211,"stargazers_count":6506,"watchers_count":6506,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":994,"mirror_url":null,"open_issues_count":336,"forks":994,"open_issues":336,"watchers":6506,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/realm/realm-java/pulls/3144"},"html":{"href":"https://github.com/realm/realm-java/pull/3144"},"issue":{"href":"https://api.github.com/repos/realm/realm-java/issues/3144"},"comments":{"href":"https://api.github.com/repos/realm/realm-java/issues/3144/comments"},"review_comments":{"href":"https://api.github.com/repos/realm/realm-java/pulls/3144/comments"},"review_comment":{"href":"https://api.github.com/repos/realm/realm-java/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/realm/realm-java/pulls/3144/commits"},"statuses":{"href":"https://api.github.com/repos/realm/realm-java/statuses/25264b73291a35d223810c80ab36c857abe695f9"}}}}
{"actor":{"display_login":"beeender"}}