{"id":15565,"date":"2021-01-21T16:19:46","date_gmt":"2021-01-21T15:19:46","guid":{"rendered":"https:\/\/daniel.haxx.se\/blog\/?p=15565"},"modified":"2021-02-24T22:36:31","modified_gmt":"2021-02-24T21:36:31","slug":"more-on-less-curl-memory","status":"publish","type":"post","link":"https:\/\/daniel.haxx.se\/blog\/2021\/01\/21\/more-on-less-curl-memory\/","title":{"rendered":"More on less curl memory"},"content":{"rendered":"\n<p><em>tldr: curl uses 30K of dynamic memory for downloading a large HTTP file, plus the size of the download buffer.<\/em><\/p>\n\n\n\n<p>Back in September 2020 I wrote about my work to trim <a href=\"https:\/\/daniel.haxx.se\/blog\/2020\/09\/24\/reducing-mallocs-for-fun\/\" data-type=\"post\" data-id=\"14740\">curl allocations done for FTP transfers<\/a>. Now I&#8217;m back again on the memory use in curl topic, from a different angle.<\/p>\n\n\n\n<p>This time, I learned about the awesome tool <a href=\"https:\/\/linux.die.net\/man\/1\/pahole\">pahole<\/a>, which can (among other things) show structs and their sizes from a built library &#8211; and when embracing this fun toy, I ran some scripts on a range of historic curl releases to get a sense of how we&#8217;re doing over time &#8211; memory size and memory allocations wise.<\/p>\n\n\n\n<p>The task I set out to myself was: <strong>figure out how the sizes of  key structs in curl have changed over time<\/strong>, and correlate that with the number and size of allocations done at run-time. To make sure that trimming down the size of a specific struct doesn&#8217;t just get allocated by another one instead, thus nullifying the gain. I  want to make sure we&#8217;re not slowly degrading &#8211; and if we do, we should at least know about it!<\/p>\n\n\n\n<p>Also: we keep developing curl at a fairly good pace and we&#8217;re adding features in almost every release. Some growth is to be expected and should be tolerated I think. We also keep the build process very configurable so users with particular needs and requirements can switch off features and thus also gain memory.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Memory sizes in modern computing<\/h2>\n\n\n\n<p>Of course systems are growing every year and machines ship with more and more ram, which also goes for the smallest machines. But there are still a vast amount of systems out there with limited memory capabilities that want good Internet transfers as well. Also, by keeping sizes down, it allows applications and systems to scale better: a 10% decrease in size <em>can<\/em> imply a 10% increase in number of possible parallel transfers. curl, and especially libcurl, is still today in 2021 frequently used on machines with limited amounts of available memory. Sometimes in the few megabytes of ram range.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Fixed configuration<\/h2>\n\n\n\n<p>In my tests I did for this I used the exact same configuration and build config for all versions tested. The sizes and behavior will vary greatly depending on config, but I tried to use a fairly complete and typical build to see how code and memory use is for &#8220;most&#8221; users. I ran everything on my x86_64 Debian Linux dev machine. My focus is on curl versions from the last 3-4 years. I figured going back to ancient times won&#8217;t help here.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Key structs<\/h2>\n\n\n\n<p><code>struct Curl_easy<\/code> &#8211; this is the &#8220;easy handle&#8221;, what is allocated by <a href=\"https:\/\/curl.se\/libcurl\/c\/curl_easy_init.html\">curl_easy_init()<\/a> and is the anchor for every transfer done with libcurl, no matter which API you&#8217;re using. An application creates one of these for each concurrent transfer it wants to do or keep around. Some applications allocate hundreds or even thousands of these.<\/p>\n\n\n\n<p><code>struct Curl_multi<\/code> &#8211; this is the &#8220;multi handle&#8221;, allocated with <a href=\"https:\/\/curl.se\/libcurl\/c\/curl_multi_init.html\">curl_multi_init()<\/a>. This handle is created by applications as a holder of many concurrent transfers so applications typically do not have a very large amount of these.<\/p>\n\n\n\n<p><code>struct connectdata<\/code> &#8211; this is an internal struct that isn&#8217;t visible externally to applications. It is the holder of connection related data for a connection to a specific server. The connection pool curl uses to handle persistent connections will hold a number of these structs in memory after the transfer has completed, to allow subsequent reuse. The size of the connection pool is customizable. A busy application doing lots of transfers might end up with a sizeable number of connections in the pool, so the size of this struct adds up.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Dynamic allocations<\/h2>\n\n\n\n<p>In early curl history, the download and upload buffers for transfers were part of the Curl_easy struct, which made it fairly large.<\/p>\n\n\n\n<p>In curl 7.53.0  (February 2017) the download buffer was turned dynamically sized and is since then allocated separately. Before that transition, curl 7.52.0 had a Curl_easy struct that was 36584 bytes, which included both the download and the upload buffers. In 7.58.0 the size was down to 21264 bytes since the download buffer was then allocated separately and was then also allowed to be done much larger than the previously set 16KB fixed size.<\/p>\n\n\n\n<p>The 16KB upload buffer was moved out of the Curl_easy handle in the 7.62.0 release (October 2018) to be done on demand &#8211; which of course especially benefits everyone who doesn&#8217;t do uploads at all&#8230; The size of this struct was then down to 6208 bytes.<\/p>\n\n\n\n<p>In curl 7.71.0 we also made the download buffer allocated on demand, and immediately freed after the transfer completes.  This makes applications that keep handles around for reuse use <em>significantly <\/em>less  memory. Applications are generally encouraged to keep the handles around to better facilitate connection reuse.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Struct size development<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Curl_easy<\/h3>\n\n\n\n<p>The size in bytes of struct <strong>Curl_easy<\/strong> the last few years:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> 7.52.0 36584\n 7.58.0 21264\n 7.61.0 21344\n 7.62.0 6208\n 7.63.0 6216\n 7.64.0 6312\n 7.65.0 5976\n 7.66.0 6024\n 7.67.0 6040\n 7.68.0 6040\n 7.69.0 6040\n 7.70.0 6080\n 7.71.0 6448\n 7.72.0 6472\n 7.73.0 6464\n 7.74.0 6512<\/pre>\n\n\n\n<p>Current git: <strong>5272<\/strong> bytes (-19% from last release). With this, <em>the struct is smaller than it has ever been before<\/em>.<\/p>\n\n\n\n<p>How we made this extra reduction? Primarily I noticed how we had a DoH related struct in the handle by default, which was <a href=\"https:\/\/github.com\/curl\/curl\/pull\/6492\">turned into on-demand allocation<\/a>. DoH is still rare and that data only needs to be allocated during the name resolving phase.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Curl_multi<\/h3>\n\n\n\n<p>The size in bytes of struct <strong>Curl_multi<\/strong> the last few years has remained very stable and it keeps being very small. Notable is that when we removed pipelining support in 7.65.0 it took away 96 bytes from this struct.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> 7.50.0 384\n 7.52.0 384\n 7.58.0 480\n 7.59.0 488\n 7.60.0 488\n 7.61.0 512\n 7.62.0 512\n 7.63.0 512\n 7.64.0 512\n<span class=\"has-inline-color has-green-color\"> 7.65.0 416<\/span>\n 7.66.0 416\n 7.67.0 424\n 7.68.0 432\n 7.69.0 416\n 7.70.0 416\n 7.71.0 416\n 7.72.0 416\n 7.73.0 416\n 7.74.0 416<\/pre>\n\n\n\n<p>Current git: <strong>416<\/strong> bytes.<\/p>\n\n\n\n<p>With this, we&#8217;re smaller than we were in the beginning of 2018.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">connectdata<\/h3>\n\n\n\n<p>The size in bytes of struct <strong>connectdata<\/strong>. It&#8217;s been both up and down.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> 7.50.0 1904 \n 7.52.0 2104 \n 7.58.0 2112 \n 7.59.0 2112 \n 7.60.0 2112 \n 7.61.0 2128 \n 7.62.0 2152 \n 7.63.0 2160 \n 7.64.0 2160 \n 7.65.0 1944 \n 7.66.0 1960 \n 7.67.0 1976 \n 7.68.0 1976 \n<span style=\"color:#a3000c\" class=\"has-inline-color\"> 7.69.0 2600 <\/span>\n 7.70.0 2608 \n 7.71.0 2608 \n 7.72.0 2624 \n 7.73.0 2640 \n 7.74.0 2656<\/pre>\n\n\n\n<p>Current git: <strong>1472<\/strong> bytes (-44% from last release)<\/p>\n\n\n\n<p>The size bump in 7.69.0 was the insertion of a new struct for state data when doing SOCKS connections non-blocking, and the corresponding decrease again for the pending release is the <a href=\"https:\/\/github.com\/curl\/curl\/pull\/6491\">removal of the buffer<\/a> from that struct. With this, we&#8217;re down to a size we had a very long time ago.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Run-time memory use<\/h2>\n\n\n\n<p>To make sure that we don&#8217;t just move memory to other on-demand buffers that we need to allocate anyway, I ran a script with a lot of curl versions and counted the number of allocations needed and the peak amount of memory allocated. For a plain 512MB download over HTTP from localhost. The counted allocations were <strong>only the ones done by curl code<\/strong> (malloc, calloc, realloc, strdup etc).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Number of allocations<\/h3>\n\n\n\n<p>There are many reasons to allocate memory and while we want to keep the number down, lots of factors of course needs to be taken into account.<\/p>\n\n\n\n<p>In the list below you&#8217;ll see that clearly we had some mistake in 7.52.0 and perhaps some more versions, as it did over 32,000 allocations. The situation was fixed in or before 7.58.0 and I haven&#8217;t bothered to go back to check exactly what it was.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span style=\"color:#a30003\" class=\"has-inline-color\"> 7.52.0 32883 <\/span>\n 7.58.0 82 \n 7.59.0 82 \n 7.60.0 82 \n 7.61.0 82 \n 7.62.0 86 \n 7.63.0 87 \n 7.64.0 87 \n 7.65.0 82\n 7.66.0 101 \n 7.67.0 107 \n 7.68.0 111 \n 7.69.0 113 \n 7.70.0 113 \n 7.71.0 99 \n 7.72.0 99 \n 7.73.0 96 \n 7.74.0 96<\/pre>\n\n\n\n<p>Current git: <strong>96<\/strong> allocations.<\/p>\n\n\n\n<p>We do more allocations than some years back, but I think it is still within a reasonable growth. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Peak memory allocations<\/h3>\n\n\n\n<p>Here&#8217;s some developments to look closer at!<\/p>\n\n\n\n<p>If we start out looking at the oldest versions in my test, we can see that they&#8217;re sub 100KB allocated &#8211; but we need to take into account the fact that back then we used a fixed 16KB download buffer. In curl 7.54.1 we bumped the default buffer size the curl tool uses to 100K which in the table below is visible in the 7.58.0 allocation.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> 7.50.0 84473 \n 7.52.0 85329 \n<span style=\"color:#a30007\" class=\"has-inline-color\"> 7.58.0 174243 <\/span>\n 7.59.0 174315 \n 7.60.0 174339 \n 7.61.0 174531 \n<span class=\"has-inline-color has-green-color\"> 7.62.0 143886 <\/span>\n 7.63.0 143928 \n 7.64.0 144128 \n 7.65.0 143152 \n 7.66.0 168188 \n 7.67.0 173365 \n 7.68.0 168575\n 7.69.0 169167\n 7.70.0 169303 \n 7.71.0 136573\n 7.72.0 136765 \n 7.73.0 136875 \n 7.74.0 137043<\/pre>\n\n\n\n<p>Current git: <strong>131680<\/strong> bytes.<\/p>\n\n\n\n<p>The gain in 7.62.0 was mostly the removal of the default allocation of the upload buffer, which isn&#8217;t used in this test&#8230;<\/p>\n\n\n\n<p>The current size tells me several things. We&#8217;re at a memory consumption level that is probably at its lowest point in the last decade &#8211; while at the same time having more features and being better than ever before. If we deduct the download buffer we have <strong>29280<\/strong> additional bytes allocated. Compare this to 7.50.0 which allocated <strong>68089<\/strong> bytes on top of the download buffer!<\/p>\n\n\n\n<p>If I change my curl to use the smallest download buffer size allowed by libcurl (1KB) instead of the default 100KB, it ends up peaking at: <strong>30304<\/strong> bytes. That&#8217;s 44% of the memory needed by 7.50.0.<\/p>\n\n\n\n<p>In my opinion, this is <em>very<\/em> good.<\/p>\n\n\n\n<p>It might also be worth to reiterate that this is with a full featured libcurl build. We can shrink even further if we switch off undesired features or just go <a href=\"https:\/\/curl.se\/tiny\/\">tiny-curl<\/a>.<\/p>\n\n\n\n<p>I hope this goes without saying, but of course all of this work has been done with the API and ABI still intact.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Graphs?<\/h2>\n\n\n\n<p>You know I like <a href=\"https:\/\/curl.se\/dashboard.html\">graphs<\/a>, but for now I decided this blog post and analysis was enough. I&#8217;m going to think about how we can perhaps get this info somehow floated on a more regular and automated way in the future. Not sure it is worth spending a lot of effort on though.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Reproduce<\/h2>\n\n\n\n<ol class=\"wp-block-list\"><li>build curl with the <code>--enable-debug<\/code> option to configure. Don&#8217;t use the threaded resolver &#8211; I use the c-ares one, because it otherwise breaks the memdebug system.<\/li><li>Run your command line with tracing enabled and then run memanalyze on the log:<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">#!\/bin\/sh\nexport CURL_MEMDEBUG=\/tmp\/curlmem.log\n.\/src\/curl -v localhost\/512M -o \/dev\/null\n.\/tests\/memanalyze.pl -v \/tmp\/curlmem.log<\/pre>\n\n\n\n<p>To get the struct sizes, just run pahole on the static libcurl lib after the build:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pahole -s lib\/.libs\/libcurl.a &gt; sizes.txt<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Credits<\/h2>\n\n\n\n<p>The photo was taken by me, in Siem Reap, Cambodia. &#8220;A smaller transport&#8221;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Updates<\/h2>\n\n\n\n<p>After the initial posting of this article I optimized the structs even further so the numbers have been updated since then to reflect the state of what&#8217;s in git a week later.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>tldr: curl uses 30K of dynamic memory for downloading a large HTTP file, plus the size of the download buffer. Back in September 2020 I wrote about my work to trim curl allocations done for FTP transfers. Now I&#8217;m back again on the memory use in curl topic, from a different angle. This time, I &hellip; <a href=\"https:\/\/daniel.haxx.se\/blog\/2021\/01\/21\/more-on-less-curl-memory\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">More on less curl memory<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":5,"featured_media":15610,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[33],"class_list":["post-15565","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-curl","tag-curl-and-libcurl"],"_links":{"self":[{"href":"https:\/\/daniel.haxx.se\/blog\/wp-json\/wp\/v2\/posts\/15565","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/daniel.haxx.se\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/daniel.haxx.se\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/daniel.haxx.se\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/daniel.haxx.se\/blog\/wp-json\/wp\/v2\/comments?post=15565"}],"version-history":[{"count":54,"href":"https:\/\/daniel.haxx.se\/blog\/wp-json\/wp\/v2\/posts\/15565\/revisions"}],"predecessor-version":[{"id":15829,"href":"https:\/\/daniel.haxx.se\/blog\/wp-json\/wp\/v2\/posts\/15565\/revisions\/15829"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/daniel.haxx.se\/blog\/wp-json\/wp\/v2\/media\/15610"}],"wp:attachment":[{"href":"https:\/\/daniel.haxx.se\/blog\/wp-json\/wp\/v2\/media?parent=15565"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/daniel.haxx.se\/blog\/wp-json\/wp\/v2\/categories?post=15565"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/daniel.haxx.se\/blog\/wp-json\/wp\/v2\/tags?post=15565"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}