{"action":"created","comment":{"url":"https://api.github.com/repos/ravendb/ravendb/pulls/comments/156759717","pull_request_review_id":83291162,"id":156759717,"diff_hunk":"@@ -149,70 +155,72 @@ public void SetNotFound(string url)\n \n         private void FreeSpace()\n         {\n+            if (!_isFreeSpaceRunning.Raise())\n+                return;\n+\n             Debug.Assert(_isFreeSpaceRunning);\n \n             if (Logger.IsInfoEnabled)\n                 Logger.Info($\"Started to clear the http cache. Items: {_items.Count}\");\n \n-            var sizeCleared = 0L;\n-            var sizeToClear = _maxSize / 4;\n-            var start = SystemTime.UtcNow;\n-\n-            var items = _items.OrderBy(x => x.Value.LastServerUpdate)\n-                .ToList();\n+            // Using the current total size will always ensure that under low memory conditions\n+            // we are making our best effort to actually get some memory back to the system in\n+            // the worst of conditions.\n+            var sizeToClear = _totalSize / 2;\n \n-            foreach (var item in items)\n+            var sizeCleared = 0L;            \n+            var start = SystemTime.UtcNow;\n+            foreach (var item in _items)\n             {\n+                // We are aggresively targetting whatever it is in our hands as \n+                // long as it havent been touched since we started to free space.\n                 var lastServerUpdate = item.Value.LastServerUpdate;\n                 if (lastServerUpdate > start)\n                     continue;\n \n-                var itemAge = SystemTime.UtcNow - lastServerUpdate;\n+                // In case that we have already achieved out target, only free those \n+                // items not having been accessed in the last minute.\n                 if (sizeCleared > sizeToClear)\n                 {\n+                    var itemAge = start - lastServerUpdate;\n                     if (itemAge.TotalMinutes <= 1)\n-                        break; // we already met the quota, and we are in new items, just drop it\n-\n-                    if (item.Value.Utilization == 0)\n-                    {\n-                        sizeCleared += FreeItem(item);\n-                    }\n+                        continue;\n                 }\n-                else\n+\n+                // We remove the item because there is no grounds to reject it.\n+                if (_items.TryRemove(item.Key, out var value) == false)\n+                    continue;\n+\n+                value.ReleaseRef();\n+\n+                if (item.Value != value)\n                 {\n-                    sizeCleared += FreeItem(item);\n+                    item.Value.ReleaseRef();\n+                    sizeCleared += item.Value.Size;\n                 }\n+\n+                sizeCleared += value.Size;\n             }\n \n             _isFreeSpaceRunning.Lower();","path":"src/Raven.Client/Http/HttpCache.cs","position":211,"original_position":211,"commit_id":"83b25c5bda4d5546d73f10a9148819ba2721c42b","original_commit_id":"83b25c5bda4d5546d73f10a9148819ba2721c42b","user":{"login":"redknightlois","id":1045411,"avatar_url":"https://avatars1.githubusercontent.com/u/1045411?v=4","gravatar_id":"","url":"https://api.github.com/users/redknightlois","html_url":"https://github.com/redknightlois","followers_url":"https://api.github.com/users/redknightlois/followers","following_url":"https://api.github.com/users/redknightlois/following{/other_user}","gists_url":"https://api.github.com/users/redknightlois/gists{/gist_id}","starred_url":"https://api.github.com/users/redknightlois/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/redknightlois/subscriptions","organizations_url":"https://api.github.com/users/redknightlois/orgs","repos_url":"https://api.github.com/users/redknightlois/repos","events_url":"https://api.github.com/users/redknightlois/events{/privacy}","received_events_url":"https://api.github.com/users/redknightlois/received_events","type":"User","site_admin":false},"body":"Yeah, it should. Correcting. ","created_at":"2017-12-13T19:33:50Z","updated_at":"2017-12-13T19:33:50Z","html_url":"https://github.com/ravendb/ravendb/pull/4695#discussion_r156759717","pull_request_url":"https://api.github.com/repos/ravendb/ravendb/pulls/4695","author_association":"MEMBER","_links":{"self":{"href":"https://api.github.com/repos/ravendb/ravendb/pulls/comments/156759717"},"html":{"href":"https://github.com/ravendb/ravendb/pull/4695#discussion_r156759717"},"pull_request":{"href":"https://api.github.com/repos/ravendb/ravendb/pulls/4695"}},"in_reply_to_id":156759009},"pull_request":{"url":"https://api.github.com/repos/ravendb/ravendb/pulls/4695","id":158178745,"html_url":"https://github.com/ravendb/ravendb/pull/4695","diff_url":"https://github.com/ravendb/ravendb/pull/4695.diff","patch_url":"https://github.com/ravendb/ravendb/pull/4695.patch","issue_url":"https://api.github.com/repos/ravendb/ravendb/issues/4695","number":4695,"state":"closed","locked":false,"title":"RavenDB-9737","user":{"login":"redknightlois","id":1045411,"avatar_url":"https://avatars1.githubusercontent.com/u/1045411?v=4","gravatar_id":"","url":"https://api.github.com/users/redknightlois","html_url":"https://github.com/redknightlois","followers_url":"https://api.github.com/users/redknightlois/followers","following_url":"https://api.github.com/users/redknightlois/following{/other_user}","gists_url":"https://api.github.com/users/redknightlois/gists{/gist_id}","starred_url":"https://api.github.com/users/redknightlois/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/redknightlois/subscriptions","organizations_url":"https://api.github.com/users/redknightlois/orgs","repos_url":"https://api.github.com/users/redknightlois/repos","events_url":"https://api.github.com/users/redknightlois/events{/privacy}","received_events_url":"https://api.github.com/users/redknightlois/received_events","type":"User","site_admin":false},"body":" 2GB Memory Capped Docker Container dies after trying to free memory repeatedly because of several potential reasons. We are overly aggresive to reserve memory and overly conservative when returning it. I balanced out both directions, being more aggresive to return and less aggresive to reserve specially when operating conditions are not the gold standard. ","created_at":"2017-12-13T19:10:24Z","updated_at":"2017-12-13T19:33:50Z","closed_at":"2017-12-13T19:31:46Z","merged_at":"2017-12-13T19:31:46Z","merge_commit_sha":"f8d471fd3c9744bf405731e97ee001a19ddbd78a","assignee":null,"assignees":[],"requested_reviewers":[],"milestone":null,"commits_url":"https://api.github.com/repos/ravendb/ravendb/pulls/4695/commits","review_comments_url":"https://api.github.com/repos/ravendb/ravendb/pulls/4695/comments","review_comment_url":"https://api.github.com/repos/ravendb/ravendb/pulls/comments{/number}","comments_url":"https://api.github.com/repos/ravendb/ravendb/issues/4695/comments","statuses_url":"https://api.github.com/repos/ravendb/ravendb/statuses/83b25c5bda4d5546d73f10a9148819ba2721c42b","head":{"label":"Corvalius:v4.0","ref":"v4.0","sha":"83b25c5bda4d5546d73f10a9148819ba2721c42b","user":{"login":"Corvalius","id":1424411,"avatar_url":"https://avatars3.githubusercontent.com/u/1424411?v=4","gravatar_id":"","url":"https://api.github.com/users/Corvalius","html_url":"https://github.com/Corvalius","followers_url":"https://api.github.com/users/Corvalius/followers","following_url":"https://api.github.com/users/Corvalius/following{/other_user}","gists_url":"https://api.github.com/users/Corvalius/gists{/gist_id}","starred_url":"https://api.github.com/users/Corvalius/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Corvalius/subscriptions","organizations_url":"https://api.github.com/users/Corvalius/orgs","repos_url":"https://api.github.com/users/Corvalius/repos","events_url":"https://api.github.com/users/Corvalius/events{/privacy}","received_events_url":"https://api.github.com/users/Corvalius/received_events","type":"Organization","site_admin":false},"repo":{"id":18448801,"name":"ravendb","full_name":"Corvalius/ravendb","owner":{"login":"Corvalius","id":1424411,"avatar_url":"https://avatars3.githubusercontent.com/u/1424411?v=4","gravatar_id":"","url":"https://api.github.com/users/Corvalius","html_url":"https://github.com/Corvalius","followers_url":"https://api.github.com/users/Corvalius/followers","following_url":"https://api.github.com/users/Corvalius/following{/other_user}","gists_url":"https://api.github.com/users/Corvalius/gists{/gist_id}","starred_url":"https://api.github.com/users/Corvalius/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Corvalius/subscriptions","organizations_url":"https://api.github.com/users/Corvalius/orgs","repos_url":"https://api.github.com/users/Corvalius/repos","events_url":"https://api.github.com/users/Corvalius/events{/privacy}","received_events_url":"https://api.github.com/users/Corvalius/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/Corvalius/ravendb","description":"A linq enabled document database for .NET","fork":true,"url":"https://api.github.com/repos/Corvalius/ravendb","forks_url":"https://api.github.com/repos/Corvalius/ravendb/forks","keys_url":"https://api.github.com/repos/Corvalius/ravendb/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Corvalius/ravendb/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Corvalius/ravendb/teams","hooks_url":"https://api.github.com/repos/Corvalius/ravendb/hooks","issue_events_url":"https://api.github.com/repos/Corvalius/ravendb/issues/events{/number}","events_url":"https://api.github.com/repos/Corvalius/ravendb/events","assignees_url":"https://api.github.com/repos/Corvalius/ravendb/assignees{/user}","branches_url":"https://api.github.com/repos/Corvalius/ravendb/branches{/branch}","tags_url":"https://api.github.com/repos/Corvalius/ravendb/tags","blobs_url":"https://api.github.com/repos/Corvalius/ravendb/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Corvalius/ravendb/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Corvalius/ravendb/git/refs{/sha}","trees_url":"https://api.github.com/repos/Corvalius/ravendb/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Corvalius/ravendb/statuses/{sha}","languages_url":"https://api.github.com/repos/Corvalius/ravendb/languages","stargazers_url":"https://api.github.com/repos/Corvalius/ravendb/stargazers","contributors_url":"https://api.github.com/repos/Corvalius/ravendb/contributors","subscribers_url":"https://api.github.com/repos/Corvalius/ravendb/subscribers","subscription_url":"https://api.github.com/repos/Corvalius/ravendb/subscription","commits_url":"https://api.github.com/repos/Corvalius/ravendb/commits{/sha}","git_commits_url":"https://api.github.com/repos/Corvalius/ravendb/git/commits{/sha}","comments_url":"https://api.github.com/repos/Corvalius/ravendb/comments{/number}","issue_comment_url":"https://api.github.com/repos/Corvalius/ravendb/issues/comments{/number}","contents_url":"https://api.github.com/repos/Corvalius/ravendb/contents/{+path}","compare_url":"https://api.github.com/repos/Corvalius/ravendb/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Corvalius/ravendb/merges","archive_url":"https://api.github.com/repos/Corvalius/ravendb/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Corvalius/ravendb/downloads","issues_url":"https://api.github.com/repos/Corvalius/ravendb/issues{/number}","pulls_url":"https://api.github.com/repos/Corvalius/ravendb/pulls{/number}","milestones_url":"https://api.github.com/repos/Corvalius/ravendb/milestones{/number}","notifications_url":"https://api.github.com/repos/Corvalius/ravendb/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Corvalius/ravendb/labels{/name}","releases_url":"https://api.github.com/repos/Corvalius/ravendb/releases{/id}","deployments_url":"https://api.github.com/repos/Corvalius/ravendb/deployments","created_at":"2014-04-04T18:48:26Z","updated_at":"2017-07-14T13:21:49Z","pushed_at":"2017-12-13T19:05:54Z","git_url":"git://github.com/Corvalius/ravendb.git","ssh_url":"git@github.com:Corvalius/ravendb.git","clone_url":"https://github.com/Corvalius/ravendb.git","svn_url":"https://github.com/Corvalius/ravendb","homepage":"http://ayende.com/Blog/","size":423624,"stargazers_count":2,"watchers_count":2,"language":"C#","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"archived":false,"open_issues_count":0,"license":{"key":"other","name":"Other","spdx_id":null,"url":null},"forks":1,"open_issues":0,"watchers":2,"default_branch":"master"}},"base":{"label":"ravendb:v4.0","ref":"v4.0","sha":"50bf887b83512c34019c3ca8a70e1cf280fd5c41","user":{"login":"ravendb","id":213792,"avatar_url":"https://avatars0.githubusercontent.com/u/213792?v=4","gravatar_id":"","url":"https://api.github.com/users/ravendb","html_url":"https://github.com/ravendb","followers_url":"https://api.github.com/users/ravendb/followers","following_url":"https://api.github.com/users/ravendb/following{/other_user}","gists_url":"https://api.github.com/users/ravendb/gists{/gist_id}","starred_url":"https://api.github.com/users/ravendb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ravendb/subscriptions","organizations_url":"https://api.github.com/users/ravendb/orgs","repos_url":"https://api.github.com/users/ravendb/repos","events_url":"https://api.github.com/users/ravendb/events{/privacy}","received_events_url":"https://api.github.com/users/ravendb/received_events","type":"Organization","site_admin":false},"repo":{"id":542714,"name":"ravendb","full_name":"ravendb/ravendb","owner":{"login":"ravendb","id":213792,"avatar_url":"https://avatars0.githubusercontent.com/u/213792?v=4","gravatar_id":"","url":"https://api.github.com/users/ravendb","html_url":"https://github.com/ravendb","followers_url":"https://api.github.com/users/ravendb/followers","following_url":"https://api.github.com/users/ravendb/following{/other_user}","gists_url":"https://api.github.com/users/ravendb/gists{/gist_id}","starred_url":"https://api.github.com/users/ravendb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ravendb/subscriptions","organizations_url":"https://api.github.com/users/ravendb/orgs","repos_url":"https://api.github.com/users/ravendb/repos","events_url":"https://api.github.com/users/ravendb/events{/privacy}","received_events_url":"https://api.github.com/users/ravendb/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/ravendb/ravendb","description":"ACID NoSQL Document Database","fork":false,"url":"https://api.github.com/repos/ravendb/ravendb","forks_url":"https://api.github.com/repos/ravendb/ravendb/forks","keys_url":"https://api.github.com/repos/ravendb/ravendb/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ravendb/ravendb/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ravendb/ravendb/teams","hooks_url":"https://api.github.com/repos/ravendb/ravendb/hooks","issue_events_url":"https://api.github.com/repos/ravendb/ravendb/issues/events{/number}","events_url":"https://api.github.com/repos/ravendb/ravendb/events","assignees_url":"https://api.github.com/repos/ravendb/ravendb/assignees{/user}","branches_url":"https://api.github.com/repos/ravendb/ravendb/branches{/branch}","tags_url":"https://api.github.com/repos/ravendb/ravendb/tags","blobs_url":"https://api.github.com/repos/ravendb/ravendb/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ravendb/ravendb/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ravendb/ravendb/git/refs{/sha}","trees_url":"https://api.github.com/repos/ravendb/ravendb/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ravendb/ravendb/statuses/{sha}","languages_url":"https://api.github.com/repos/ravendb/ravendb/languages","stargazers_url":"https://api.github.com/repos/ravendb/ravendb/stargazers","contributors_url":"https://api.github.com/repos/ravendb/ravendb/contributors","subscribers_url":"https://api.github.com/repos/ravendb/ravendb/subscribers","subscription_url":"https://api.github.com/repos/ravendb/ravendb/subscription","commits_url":"https://api.github.com/repos/ravendb/ravendb/commits{/sha}","git_commits_url":"https://api.github.com/repos/ravendb/ravendb/git/commits{/sha}","comments_url":"https://api.github.com/repos/ravendb/ravendb/comments{/number}","issue_comment_url":"https://api.github.com/repos/ravendb/ravendb/issues/comments{/number}","contents_url":"https://api.github.com/repos/ravendb/ravendb/contents/{+path}","compare_url":"https://api.github.com/repos/ravendb/ravendb/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ravendb/ravendb/merges","archive_url":"https://api.github.com/repos/ravendb/ravendb/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ravendb/ravendb/downloads","issues_url":"https://api.github.com/repos/ravendb/ravendb/issues{/number}","pulls_url":"https://api.github.com/repos/ravendb/ravendb/pulls{/number}","milestones_url":"https://api.github.com/repos/ravendb/ravendb/milestones{/number}","notifications_url":"https://api.github.com/repos/ravendb/ravendb/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ravendb/ravendb/labels{/name}","releases_url":"https://api.github.com/repos/ravendb/ravendb/releases{/id}","deployments_url":"https://api.github.com/repos/ravendb/ravendb/deployments","created_at":"2010-03-02T11:05:30Z","updated_at":"2017-12-13T19:15:41Z","pushed_at":"2017-12-13T19:32:21Z","git_url":"git://github.com/ravendb/ravendb.git","ssh_url":"git@github.com:ravendb/ravendb.git","clone_url":"https://github.com/ravendb/ravendb.git","svn_url":"https://github.com/ravendb/ravendb","homepage":"http://ravendb.net","size":422716,"stargazers_count":1787,"watchers_count":1787,"language":"C#","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":674,"mirror_url":null,"archived":false,"open_issues_count":2,"license":null,"forks":674,"open_issues":2,"watchers":1787,"default_branch":"v4.0"}},"_links":{"self":{"href":"https://api.github.com/repos/ravendb/ravendb/pulls/4695"},"html":{"href":"https://github.com/ravendb/ravendb/pull/4695"},"issue":{"href":"https://api.github.com/repos/ravendb/ravendb/issues/4695"},"comments":{"href":"https://api.github.com/repos/ravendb/ravendb/issues/4695/comments"},"review_comments":{"href":"https://api.github.com/repos/ravendb/ravendb/pulls/4695/comments"},"review_comment":{"href":"https://api.github.com/repos/ravendb/ravendb/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/ravendb/ravendb/pulls/4695/commits"},"statuses":{"href":"https://api.github.com/repos/ravendb/ravendb/statuses/83b25c5bda4d5546d73f10a9148819ba2721c42b"}},"author_association":"MEMBER"}}
{"actor":{"display_login":"redknightlois"}}