| |
| Posted by Vladimir Panteleev | PermalinkReply |
|
Vladimir Panteleev
| https://issues.dlang.org/show_bug.cgi?id=24328
--- Comment #1 from Vladimir Panteleev <dlang-bugzilla@thecybershadow.net> ---
If I modify the program to use C malloc/free instead of the D GC, I get 81% utilization:
////////////////////////// test_c.d //////////////////////////
import core.stdc.stdio;
import core.stdc.stdlib;
enum size_t memoryLimit = 1UL * 1024 * 1024 * 1024;
enum size_t allocationSize = 64;
@nogc
void main()
{
{
import core.sys.posix.sys.resource :
rlimit, RLIMIT_AS, getrlimit, setrlimit;
rlimit lim;
getrlimit(RLIMIT_AS, &lim);
lim.rlim_cur = memoryLimit;
setrlimit(RLIMIT_AS, &lim);
}
// Size of reachable data created by this function
size_t utilizedSize;
scope(exit) printf("Utilized size: %zu (%zd%%)\n",
utilizedSize, utilizedSize * 100 / memoryLimit);
void** pinned; size_t numPinned;
int n;
while (true)
{
void* arr = malloc(allocationSize);
if (!arr)
{
printf("Failed to allocate %zu\n",
allocationSize);
return;
}
bool keep = n++ % 2 == 0;
if (keep)
{
auto newSize = (numPinned + 1) * pinned[0].sizeof;
pinned = cast(void**)realloc(pinned, newSize);
if (!pinned)
{
printf("Failed to reallocate %zu bytes\n",
(numPinned + 1) * pinned[0].sizeof);
return;
}
pinned[numPinned++] = arr;
utilizedSize += allocationSize + pinned[0].sizeof;
}
else
free(arr);
}
}
//////////////////////////////////////////////////////////////
--
|