{"action":"created","comment":{"url":"https://api.github.com/repos/novoda/download-manager/pulls/comments/35309981","id":35309981,"diff_hunk":"@@ -0,0 +1,36 @@\n+package com.novoda.downloadmanager.lib;\n+\n+import android.os.SystemClock;\n+\n+import java.util.concurrent.TimeUnit;\n+\n+class Clock {\n+\n+    public enum Interval {","path":"library/src/main/java/com/novoda/downloadmanager/lib/Clock.java","position":9,"original_position":9,"commit_id":"f04bc717c6792fe088c3df7520d2926720ae2f2c","original_commit_id":"f04bc717c6792fe088c3df7520d2926720ae2f2c","user":{"login":"zegnus","id":2845931,"avatar_url":"https://avatars.githubusercontent.com/u/2845931?v=3","gravatar_id":"","url":"https://api.github.com/users/zegnus","html_url":"https://github.com/zegnus","followers_url":"https://api.github.com/users/zegnus/followers","following_url":"https://api.github.com/users/zegnus/following{/other_user}","gists_url":"https://api.github.com/users/zegnus/gists{/gist_id}","starred_url":"https://api.github.com/users/zegnus/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/zegnus/subscriptions","organizations_url":"https://api.github.com/users/zegnus/orgs","repos_url":"https://api.github.com/users/zegnus/repos","events_url":"https://api.github.com/users/zegnus/events{/privacy}","received_events_url":"https://api.github.com/users/zegnus/received_events","type":"User","site_admin":false},"body":"No I haven't but the problem in this case is the database query, not the code by itself.\r\n\r\nI mean for example\r\n```java\r\nif (controlStatus.isPaused())\r\n```\r\n\r\nWould be slower than\r\n```java\r\nif (controlStatus.isPaused)\r\n```\r\n\r\nBut those kinds of micro-optimisations are not really necessary here for now :) The db is the big problem :)","created_at":"2015-07-23T10:51:20Z","updated_at":"2015-07-23T10:51:20Z","html_url":"https://github.com/novoda/download-manager/pull/129#discussion_r35309981","pull_request_url":"https://api.github.com/repos/novoda/download-manager/pulls/129","_links":{"self":{"href":"https://api.github.com/repos/novoda/download-manager/pulls/comments/35309981"},"html":{"href":"https://github.com/novoda/download-manager/pull/129#discussion_r35309981"},"pull_request":{"href":"https://api.github.com/repos/novoda/download-manager/pulls/129"}}},"pull_request":{"url":"https://api.github.com/repos/novoda/download-manager/pulls/129","id":40657779,"html_url":"https://github.com/novoda/download-manager/pull/129","diff_url":"https://github.com/novoda/download-manager/pull/129.diff","patch_url":"https://github.com/novoda/download-manager/pull/129.patch","issue_url":"https://api.github.com/repos/novoda/download-manager/issues/129","number":129,"state":"open","locked":false,"title":"Avoid smashing the database while transferring bytes","user":{"login":"zegnus","id":2845931,"avatar_url":"https://avatars.githubusercontent.com/u/2845931?v=3","gravatar_id":"","url":"https://api.github.com/users/zegnus","html_url":"https://github.com/zegnus","followers_url":"https://api.github.com/users/zegnus/followers","following_url":"https://api.github.com/users/zegnus/following{/other_user}","gists_url":"https://api.github.com/users/zegnus/gists{/gist_id}","starred_url":"https://api.github.com/users/zegnus/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/zegnus/subscriptions","organizations_url":"https://api.github.com/users/zegnus/orgs","repos_url":"https://api.github.com/users/zegnus/repos","events_url":"https://api.github.com/users/zegnus/events{/privacy}","received_events_url":"https://api.github.com/users/zegnus/received_events","type":"User","site_admin":false},"body":"## Task description ##\r\n\r\nI've noticed that the GC goes crazy while downloading\r\n\r\n## Solution implemented ##\r\n\r\nAt every HTTP byte transfer there is a check for paused or cancelled downloads that leads to a database query. Even though the query is small, is too frequent.\r\n\r\nThe solution implemented prevents fast queries in series to the database preventing more than one query per second.\r\n\r\nThis will allow a check of paused or cancelled inside a time frame of 1s that is good enough and reduces dramatically the GC\r\n\r\n**Screenshots**\r\n\r\nBefore | After\r\n--- | ---\r\n<img width=\"1440\" alt=\"old-memory\" src=\"https://cloud.githubusercontent.com/assets/2845931/8848309/80905410-3136-11e5-98ba-ffb417530e34.png\"> | <img width=\"1440\" alt=\"new-memory\" src=\"https://cloud.githubusercontent.com/assets/2845931/8848314/87cb8f56-3136-11e5-9536-26c7994f723f.png\">\r\n<img width=\"1440\" alt=\"old-cpu\" src=\"https://cloud.githubusercontent.com/assets/2845931/8848320/9162f16c-3136-11e5-8292-a1375fbd90aa.png\"> | <img width=\"1440\" alt=\"new-cpu\" src=\"https://cloud.githubusercontent.com/assets/2845931/8848323/98f4a1e6-3136-11e5-9401-a1af2c3cb270.png\">\r\n\r\n**Before small log snipped for GC logs**\r\n```\r\n07-23 11:12:56.819  15125-15183/com.novoda.downloadmanager.demo.extended I/art﹕ Explicit concurrent mark sweep GC freed 107078(5MB) AllocSpace objects, 0(0B) LOS objects, 40% free, 20MB/34MB, paused 557us total 15.386ms\r\n07-23 11:12:57.607  15125-15183/com.novoda.downloadmanager.demo.extended I/art﹕ Explicit concurrent mark sweep GC freed 109726(5MB) AllocSpace objects, 1(1302KB) LOS objects, 40% free, 19MB/32MB, paused 896us total 13.422ms\r\n07-23 11:12:58.247  15125-15183/com.novoda.downloadmanager.demo.extended I/art﹕ Explicit concurrent mark sweep GC freed 109045(5MB) AllocSpace objects, 0(0B) LOS objects, 40% free, 20MB/34MB, paused 679us total 14.400ms\r\n07-23 11:12:58.945  15125-15183/com.novoda.downloadmanager.demo.extended I/art﹕ Explicit concurrent mark sweep GC freed 107889(5MB) AllocSpace objects, 1(1302KB) LOS objects, 39% free, 19MB/32MB, paused 503us total 29.020ms\r\n07-23 11:12:59.616  15125-15183/com.novoda.downloadmanager.demo.extended I/art﹕ Explicit concurrent mark sweep GC freed 107429(5MB) AllocSpace objects, 0(0B) LOS objects, 40% free, 20MB/34MB, paused 812us total 13.052ms\r\n07-23 11:13:00.381  15125-15183/com.novoda.downloadmanager.demo.extended I/art﹕ Explicit concurrent mark sweep GC freed 108781(5MB) AllocSpace objects, 1(1302KB) LOS objects, 39% free, 19MB/32MB, paused 1.425ms total 14.065ms\r\n07-23 11:13:02.315  15125-15183/com.novoda.downloadmanager.demo.extended I/art﹕ Explicit concurrent mark sweep GC freed 110088(5MB) AllocSpace objects, 0(0B) LOS objects, 40% free, 20MB/34MB, paused 1.049ms total 24.511ms\r\n07-23 11:13:02.985  15125-15183/com.novoda.downloadmanager.demo.extended I/art﹕ Explicit concurrent mark sweep GC freed 108935(5MB) AllocSpace objects, 1(1302KB) LOS objects, 39% free, 19MB/32MB, paused 583us total 18.004ms\r\n```\r\n\r\n**After full GC logs**\r\n```\r\n07-23 11:16:17.906  17002-17060/com.novoda.downloadmanager.demo.extended I/NoTils﹕ [pool-2-thread-1][DownloadThread.runInternal:258] Download 27 starting\r\n07-23 11:17:15.863  17002-17017/com.novoda.downloadmanager.demo.extended I/art﹕ Background partial concurrent mark sweep GC freed 8859(467KB) AllocSpace objects, 12(15MB) LOS objects, 40% free, 22MB/38MB, paused 6.087ms total 22.389ms\r\n07-23 11:19:19.355  17002-17060/com.novoda.downloadmanager.demo.extended I/NoTils﹕ [pool-2-thread-1][DownloadThread.runInternal:330] Download 27 finished with status SUCCESS\r\n```\r\n","created_at":"2015-07-23T10:33:04Z","updated_at":"2015-07-23T10:51:20Z","closed_at":null,"merged_at":null,"merge_commit_sha":"c7a7a58f95d2a712bcddeb6c15f5292698a3569a","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/novoda/download-manager/pulls/129/commits","review_comments_url":"https://api.github.com/repos/novoda/download-manager/pulls/129/comments","review_comment_url":"https://api.github.com/repos/novoda/download-manager/pulls/comments{/number}","comments_url":"https://api.github.com/repos/novoda/download-manager/issues/129/comments","statuses_url":"https://api.github.com/repos/novoda/download-manager/statuses/f04bc717c6792fe088c3df7520d2926720ae2f2c","head":{"label":"novoda:Reduce-GC","ref":"Reduce-GC","sha":"f04bc717c6792fe088c3df7520d2926720ae2f2c","user":{"login":"novoda","id":74874,"avatar_url":"https://avatars.githubusercontent.com/u/74874?v=3","gravatar_id":"","url":"https://api.github.com/users/novoda","html_url":"https://github.com/novoda","followers_url":"https://api.github.com/users/novoda/followers","following_url":"https://api.github.com/users/novoda/following{/other_user}","gists_url":"https://api.github.com/users/novoda/gists{/gist_id}","starred_url":"https://api.github.com/users/novoda/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/novoda/subscriptions","organizations_url":"https://api.github.com/users/novoda/orgs","repos_url":"https://api.github.com/users/novoda/repos","events_url":"https://api.github.com/users/novoda/events{/privacy}","received_events_url":"https://api.github.com/users/novoda/received_events","type":"Organization","site_admin":false},"repo":{"id":35211795,"name":"download-manager","full_name":"novoda/download-manager","owner":{"login":"novoda","id":74874,"avatar_url":"https://avatars.githubusercontent.com/u/74874?v=3","gravatar_id":"","url":"https://api.github.com/users/novoda","html_url":"https://github.com/novoda","followers_url":"https://api.github.com/users/novoda/followers","following_url":"https://api.github.com/users/novoda/following{/other_user}","gists_url":"https://api.github.com/users/novoda/gists{/gist_id}","starred_url":"https://api.github.com/users/novoda/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/novoda/subscriptions","organizations_url":"https://api.github.com/users/novoda/orgs","repos_url":"https://api.github.com/users/novoda/repos","events_url":"https://api.github.com/users/novoda/events{/privacy}","received_events_url":"https://api.github.com/users/novoda/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/novoda/download-manager","description":"Download manager based on AOSP DM but allowing downloading to internal private storage","fork":false,"url":"https://api.github.com/repos/novoda/download-manager","forks_url":"https://api.github.com/repos/novoda/download-manager/forks","keys_url":"https://api.github.com/repos/novoda/download-manager/keys{/key_id}","collaborators_url":"https://api.github.com/repos/novoda/download-manager/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/novoda/download-manager/teams","hooks_url":"https://api.github.com/repos/novoda/download-manager/hooks","issue_events_url":"https://api.github.com/repos/novoda/download-manager/issues/events{/number}","events_url":"https://api.github.com/repos/novoda/download-manager/events","assignees_url":"https://api.github.com/repos/novoda/download-manager/assignees{/user}","branches_url":"https://api.github.com/repos/novoda/download-manager/branches{/branch}","tags_url":"https://api.github.com/repos/novoda/download-manager/tags","blobs_url":"https://api.github.com/repos/novoda/download-manager/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/novoda/download-manager/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/novoda/download-manager/git/refs{/sha}","trees_url":"https://api.github.com/repos/novoda/download-manager/git/trees{/sha}","statuses_url":"https://api.github.com/repos/novoda/download-manager/statuses/{sha}","languages_url":"https://api.github.com/repos/novoda/download-manager/languages","stargazers_url":"https://api.github.com/repos/novoda/download-manager/stargazers","contributors_url":"https://api.github.com/repos/novoda/download-manager/contributors","subscribers_url":"https://api.github.com/repos/novoda/download-manager/subscribers","subscription_url":"https://api.github.com/repos/novoda/download-manager/subscription","commits_url":"https://api.github.com/repos/novoda/download-manager/commits{/sha}","git_commits_url":"https://api.github.com/repos/novoda/download-manager/git/commits{/sha}","comments_url":"https://api.github.com/repos/novoda/download-manager/comments{/number}","issue_comment_url":"https://api.github.com/repos/novoda/download-manager/issues/comments{/number}","contents_url":"https://api.github.com/repos/novoda/download-manager/contents/{+path}","compare_url":"https://api.github.com/repos/novoda/download-manager/compare/{base}...{head}","merges_url":"https://api.github.com/repos/novoda/download-manager/merges","archive_url":"https://api.github.com/repos/novoda/download-manager/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/novoda/download-manager/downloads","issues_url":"https://api.github.com/repos/novoda/download-manager/issues{/number}","pulls_url":"https://api.github.com/repos/novoda/download-manager/pulls{/number}","milestones_url":"https://api.github.com/repos/novoda/download-manager/milestones{/number}","notifications_url":"https://api.github.com/repos/novoda/download-manager/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/novoda/download-manager/labels{/name}","releases_url":"https://api.github.com/repos/novoda/download-manager/releases{/id}","created_at":"2015-05-07T09:41:52Z","updated_at":"2015-07-23T09:24:36Z","pushed_at":"2015-07-23T10:33:05Z","git_url":"git://github.com/novoda/download-manager.git","ssh_url":"git@github.com:novoda/download-manager.git","clone_url":"https://github.com/novoda/download-manager.git","svn_url":"https://github.com/novoda/download-manager","homepage":"","size":4185,"stargazers_count":57,"watchers_count":57,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":5,"mirror_url":null,"open_issues_count":21,"forks":5,"open_issues":21,"watchers":57,"default_branch":"master"}},"base":{"label":"novoda:master","ref":"master","sha":"e0ebc82b340a4acd3ac01d06436504710afc8ab7","user":{"login":"novoda","id":74874,"avatar_url":"https://avatars.githubusercontent.com/u/74874?v=3","gravatar_id":"","url":"https://api.github.com/users/novoda","html_url":"https://github.com/novoda","followers_url":"https://api.github.com/users/novoda/followers","following_url":"https://api.github.com/users/novoda/following{/other_user}","gists_url":"https://api.github.com/users/novoda/gists{/gist_id}","starred_url":"https://api.github.com/users/novoda/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/novoda/subscriptions","organizations_url":"https://api.github.com/users/novoda/orgs","repos_url":"https://api.github.com/users/novoda/repos","events_url":"https://api.github.com/users/novoda/events{/privacy}","received_events_url":"https://api.github.com/users/novoda/received_events","type":"Organization","site_admin":false},"repo":{"id":35211795,"name":"download-manager","full_name":"novoda/download-manager","owner":{"login":"novoda","id":74874,"avatar_url":"https://avatars.githubusercontent.com/u/74874?v=3","gravatar_id":"","url":"https://api.github.com/users/novoda","html_url":"https://github.com/novoda","followers_url":"https://api.github.com/users/novoda/followers","following_url":"https://api.github.com/users/novoda/following{/other_user}","gists_url":"https://api.github.com/users/novoda/gists{/gist_id}","starred_url":"https://api.github.com/users/novoda/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/novoda/subscriptions","organizations_url":"https://api.github.com/users/novoda/orgs","repos_url":"https://api.github.com/users/novoda/repos","events_url":"https://api.github.com/users/novoda/events{/privacy}","received_events_url":"https://api.github.com/users/novoda/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/novoda/download-manager","description":"Download manager based on AOSP DM but allowing downloading to internal private storage","fork":false,"url":"https://api.github.com/repos/novoda/download-manager","forks_url":"https://api.github.com/repos/novoda/download-manager/forks","keys_url":"https://api.github.com/repos/novoda/download-manager/keys{/key_id}","collaborators_url":"https://api.github.com/repos/novoda/download-manager/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/novoda/download-manager/teams","hooks_url":"https://api.github.com/repos/novoda/download-manager/hooks","issue_events_url":"https://api.github.com/repos/novoda/download-manager/issues/events{/number}","events_url":"https://api.github.com/repos/novoda/download-manager/events","assignees_url":"https://api.github.com/repos/novoda/download-manager/assignees{/user}","branches_url":"https://api.github.com/repos/novoda/download-manager/branches{/branch}","tags_url":"https://api.github.com/repos/novoda/download-manager/tags","blobs_url":"https://api.github.com/repos/novoda/download-manager/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/novoda/download-manager/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/novoda/download-manager/git/refs{/sha}","trees_url":"https://api.github.com/repos/novoda/download-manager/git/trees{/sha}","statuses_url":"https://api.github.com/repos/novoda/download-manager/statuses/{sha}","languages_url":"https://api.github.com/repos/novoda/download-manager/languages","stargazers_url":"https://api.github.com/repos/novoda/download-manager/stargazers","contributors_url":"https://api.github.com/repos/novoda/download-manager/contributors","subscribers_url":"https://api.github.com/repos/novoda/download-manager/subscribers","subscription_url":"https://api.github.com/repos/novoda/download-manager/subscription","commits_url":"https://api.github.com/repos/novoda/download-manager/commits{/sha}","git_commits_url":"https://api.github.com/repos/novoda/download-manager/git/commits{/sha}","comments_url":"https://api.github.com/repos/novoda/download-manager/comments{/number}","issue_comment_url":"https://api.github.com/repos/novoda/download-manager/issues/comments{/number}","contents_url":"https://api.github.com/repos/novoda/download-manager/contents/{+path}","compare_url":"https://api.github.com/repos/novoda/download-manager/compare/{base}...{head}","merges_url":"https://api.github.com/repos/novoda/download-manager/merges","archive_url":"https://api.github.com/repos/novoda/download-manager/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/novoda/download-manager/downloads","issues_url":"https://api.github.com/repos/novoda/download-manager/issues{/number}","pulls_url":"https://api.github.com/repos/novoda/download-manager/pulls{/number}","milestones_url":"https://api.github.com/repos/novoda/download-manager/milestones{/number}","notifications_url":"https://api.github.com/repos/novoda/download-manager/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/novoda/download-manager/labels{/name}","releases_url":"https://api.github.com/repos/novoda/download-manager/releases{/id}","created_at":"2015-05-07T09:41:52Z","updated_at":"2015-07-23T09:24:36Z","pushed_at":"2015-07-23T10:33:05Z","git_url":"git://github.com/novoda/download-manager.git","ssh_url":"git@github.com:novoda/download-manager.git","clone_url":"https://github.com/novoda/download-manager.git","svn_url":"https://github.com/novoda/download-manager","homepage":"","size":4185,"stargazers_count":57,"watchers_count":57,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":5,"mirror_url":null,"open_issues_count":21,"forks":5,"open_issues":21,"watchers":57,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/novoda/download-manager/pulls/129"},"html":{"href":"https://github.com/novoda/download-manager/pull/129"},"issue":{"href":"https://api.github.com/repos/novoda/download-manager/issues/129"},"comments":{"href":"https://api.github.com/repos/novoda/download-manager/issues/129/comments"},"review_comments":{"href":"https://api.github.com/repos/novoda/download-manager/pulls/129/comments"},"review_comment":{"href":"https://api.github.com/repos/novoda/download-manager/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/novoda/download-manager/pulls/129/commits"},"statuses":{"href":"https://api.github.com/repos/novoda/download-manager/statuses/f04bc717c6792fe088c3df7520d2926720ae2f2c"}}}}