{"action":"created","issue":{"url":"https://api.github.com/repos/dotnet/coreclr/issues/1235","labels_url":"https://api.github.com/repos/dotnet/coreclr/issues/1235/labels{/name}","comments_url":"https://api.github.com/repos/dotnet/coreclr/issues/1235/comments","events_url":"https://api.github.com/repos/dotnet/coreclr/issues/1235/events","html_url":"https://github.com/dotnet/coreclr/issues/1235","id":94956475,"number":1235,"title":"Custom allocators - (size, disposal, etc).","user":{"login":"ayende","id":116915,"avatar_url":"https://avatars.githubusercontent.com/u/116915?v=3","gravatar_id":"","url":"https://api.github.com/users/ayende","html_url":"https://github.com/ayende","followers_url":"https://api.github.com/users/ayende/followers","following_url":"https://api.github.com/users/ayende/following{/other_user}","gists_url":"https://api.github.com/users/ayende/gists{/gist_id}","starred_url":"https://api.github.com/users/ayende/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ayende/subscriptions","organizations_url":"https://api.github.com/users/ayende/orgs","repos_url":"https://api.github.com/users/ayende/repos","events_url":"https://api.github.com/users/ayende/events{/privacy}","received_events_url":"https://api.github.com/users/ayende/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/dotnet/coreclr/labels/performance","name":"performance","color":"5319e7"}],"state":"open","locked":false,"assignee":{"login":"brianrob","id":6210322,"avatar_url":"https://avatars.githubusercontent.com/u/6210322?v=3","gravatar_id":"","url":"https://api.github.com/users/brianrob","html_url":"https://github.com/brianrob","followers_url":"https://api.github.com/users/brianrob/followers","following_url":"https://api.github.com/users/brianrob/following{/other_user}","gists_url":"https://api.github.com/users/brianrob/gists{/gist_id}","starred_url":"https://api.github.com/users/brianrob/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/brianrob/subscriptions","organizations_url":"https://api.github.com/users/brianrob/orgs","repos_url":"https://api.github.com/users/brianrob/repos","events_url":"https://api.github.com/users/brianrob/events{/privacy}","received_events_url":"https://api.github.com/users/brianrob/received_events","type":"User","site_admin":false},"milestone":{"url":"https://api.github.com/repos/dotnet/coreclr/milestones/5","html_url":"https://github.com/dotnet/coreclr/milestones/Future","labels_url":"https://api.github.com/repos/dotnet/coreclr/milestones/5/labels","id":1328081,"number":5,"title":"Future","description":"Milestone tracking the issues that have been triaged for after 1.0.0-rtm","creator":{"login":"joshfree","id":1302850,"avatar_url":"https://avatars.githubusercontent.com/u/1302850?v=3","gravatar_id":"","url":"https://api.github.com/users/joshfree","html_url":"https://github.com/joshfree","followers_url":"https://api.github.com/users/joshfree/followers","following_url":"https://api.github.com/users/joshfree/following{/other_user}","gists_url":"https://api.github.com/users/joshfree/gists{/gist_id}","starred_url":"https://api.github.com/users/joshfree/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/joshfree/subscriptions","organizations_url":"https://api.github.com/users/joshfree/orgs","repos_url":"https://api.github.com/users/joshfree/repos","events_url":"https://api.github.com/users/joshfree/events{/privacy}","received_events_url":"https://api.github.com/users/joshfree/received_events","type":"User","site_admin":false},"open_issues":163,"closed_issues":2,"state":"open","created_at":"2015-09-29T00:26:07Z","updated_at":"2015-11-11T05:50:44Z","due_on":null,"closed_at":null},"comments":34,"created_at":"2015-07-14T14:26:43Z","updated_at":"2015-11-11T12:02:59Z","closed_at":null,"body":"One of the hardest things that we have to handle when writing server application or system software in .NET is the fact that we don't have good control over memory.\r\nThis range from simple inability to state \"how big is this thing\" to controlling how much memory we'll use for certain operations.\r\n\r\nIn my case, working on RavenDB, there are a lot of operations that require extensive memory usage, over which we have little control. The user can specify any indexing function they want, and we'll have to respect that.\r\nWhen doing heavy indexing, that can cause several issues. In particular, it means that we generate a LOT of relatively short term data, during which other operations also run. Because we are system software, we are doing a lot of I/O, which require pinning memory.\r\n\r\nThe end result is that we may have memory with the following layout.\r\n\r\n[useful objects] [ indexing garbage ] [pinned buffers doing i/o] [ indexing garbage] [ pinned buffers ]\r\n\r\nThat result in high memory fragmentation (on Gen0, mostly), which is hard to deal with.\r\n\r\nIt also means that when indexing is done, we have to cleanup quite a lot of garbage, and because the indexing garbage is mixed with stuff that is used right now, it either cannot be readily allocated or require a big compaction.\r\n\r\nIt would be great if we had greater control over memory usage. Being able to define a heap and instruct the CLR to allocate objects from it would be wonderful. We won't have request processing memory intermixed with background ops memory, and we have a good way to free a lot of memory all at once.\r\n\r\nOne option would be to do something like this:\r\n\r\n    using(var heap = Heap.Create(HeapOptions.None, \r\n        1024 * 1024, // initial size\r\n        512 * 1024 * 1024)) // max size\r\n    {\r\n\r\n      using(heap.AllocateFromMe())\r\n      {\r\n         var sb = new StringBuilder();\r\n         for(var i = 0; i < 100; i ++ )\r\n               sb.AppendLine(i);\r\n         Console.WriteLine(sb.ToString());\r\n      }\r\n    }\r\n\r\nThis will ensure that all allocations inside the scope are allocated on the new heap. The GC isn't involved in collecting items from this heap at all, it is the responsibility of the user to take care of that, either by explicitly freeing objects (rare) or by disposing the heap.\r\n\r\nUsage of references to the heap after it is destroyed won't be allowed.\r\n\r\nAlternatively, because that might be too hard / complex. Just having a way to do something like:\r\n\r\n     heap.Allocate<MyItem>();\r\n\r\nWould be great. Note that we can do the same right now by allocating native memory and using unsafe code to get a struct pointer back. This works, but very common types like arrays or strings cannot be allocated in this manner. \r\n\r\nHaving support for explicit usage like this would greatly alleviate the kind of gymnastics that we have to go through to manage memory.\r\n "},"comment":{"url":"https://api.github.com/repos/dotnet/coreclr/issues/comments/155758223","html_url":"https://github.com/dotnet/coreclr/issues/1235#issuecomment-155758223","issue_url":"https://api.github.com/repos/dotnet/coreclr/issues/1235","id":155758223,"user":{"login":"mgravell","id":17328,"avatar_url":"https://avatars.githubusercontent.com/u/17328?v=3","gravatar_id":"","url":"https://api.github.com/users/mgravell","html_url":"https://github.com/mgravell","followers_url":"https://api.github.com/users/mgravell/followers","following_url":"https://api.github.com/users/mgravell/following{/other_user}","gists_url":"https://api.github.com/users/mgravell/gists{/gist_id}","starred_url":"https://api.github.com/users/mgravell/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mgravell/subscriptions","organizations_url":"https://api.github.com/users/mgravell/orgs","repos_url":"https://api.github.com/users/mgravell/repos","events_url":"https://api.github.com/users/mgravell/events{/privacy}","received_events_url":"https://api.github.com/users/mgravell/received_events","type":"User","site_admin":false},"created_at":"2015-11-11T12:02:59Z","updated_at":"2015-11-11T12:02:59Z","body":"Have you seen the corefxlab stuff? In particular `System.Buffers`; this is all just speculative ideas, of course, not actual production stuff: https://github.com/dotnet/corefxlab/tree/master/src/System.Buffers"}}