{"id":4319,"date":"2024-06-17T09:19:08","date_gmt":"2024-06-17T03:49:08","guid":{"rendered":"https:\/\/cthecosmos.com\/?p=4319"},"modified":"2024-06-17T09:19:36","modified_gmt":"2024-06-17T03:49:36","slug":"a-deep-dive-into-memory","status":"publish","type":"post","link":"https:\/\/cthecosmos.com\/?p=4319","title":{"rendered":"A Deep Dive Into Memory"},"content":{"rendered":"\n<div class=\"wp-block-group is-vertical is-layout-flex wp-container-core-group-is-layout-8cf370e7 wp-block-group-is-layout-flex\">\n<div class=\"wp-block-group is-vertical is-layout-flex wp-container-core-group-is-layout-8cf370e7 wp-block-group-is-layout-flex\">\n<div class=\"wp-block-group is-vertical is-layout-flex wp-container-core-group-is-layout-8cf370e7 wp-block-group-is-layout-flex\">\n<p class=\"has-black-color has-white-background-color has-text-color has-background has-link-color wp-elements-25e371a41176db3d9d1e96f9991b1ab8\" style=\"font-size:17px;line-height:1.8\">On my journey to understand memory allocation techniques and how memory works behind the user space, I am getting started with an understanding of malloc. <strong>Malloc()<\/strong>, <strong>Calloc()<\/strong>, <strong>Realloc()<\/strong> and <strong>Free()<\/strong> are the Dynamic memory allocation functions present in <strong>stdlib.h<\/strong> .<br><br>Throughout the years, various revisions have been implemented on malloc and only one among them stands out. In the late<strong> 1980, Doug Lea<\/strong> developed an improved version of <strong>malloc()<\/strong> known as dlmalloc(). It became the basis for the malloc() used in the GNU C library. This version introduced several optimizations and became widely adopted due to its efficiency.<br><br>Apart from<strong> dlmalloc(),<\/strong> there are other versions of malloc; malloc() that was developed for FreeBSD, <strong>tcmalloc()<\/strong> which is a part of googles performance tools, <strong>ptmalloc()<\/strong> that is an adaption of <strong>dlmalloc()<\/strong> to support multiple threads without locks which was later incorporated into glibc. The latest versions of malloc runs on the implementation of <strong>ptmalloc2()<\/strong>.<br><br>Whenever the user calls <strong>malloc()<\/strong> or any dynamic memory related functions, behind the scenes of them, the system calls<strong> brk()\/sbrk() <\/strong>and <strong>mmap()<\/strong> are called in order to execute the required action; allocate memory in this case.<br><br>The main actions that are performed on a top level are:<br><br><strong>Determine Available Memory<\/strong>: Before allocating memory, the system checks if there&#8217;s enough free memory to fulfill the request. If not, either the request is denied, or some memory management magic is performed (like using swap space).<br><br><strong>Allocate Memory<\/strong>: When a program or a part of a program needs memory, it requests aspecific amount. The system then finds a contiguous block of memory of the requested size and marks it as in use. This reserved memory is then handed over to the requester.<br><br><strong>Deallocate Memory<\/strong>: Once the program is done using the allocated memory, it&#8217;s essential to return it to the system. This process releases the previously reserved memory, making it available for future allocations either by the same program or by other programs.<br><br><br><strong>brk(): brk()<\/strong> is used to change the location of the program break, which defines the end of theprocess&#8217;s data segment (i.e., the program&#8217;s heap). Changing the program break to a higher value increases the amount of heap memory available, while setting it to a lower value decreases it.<br><br><strong><em>int brk(void *end_data_segment);<\/em><\/strong><br><br><strong>end_data_segment<\/strong>: The new end of the data segment (i.e., the new program break).<br><br><strong>Returns<\/strong>: On success, zero is returned. On error, -1 is returned, and errno is set to ENOMEM.<br><br><strong>sbrk():<\/strong> changes the program break by the value specified. It&#8217;s a more convenient way to allocate or deallocate memory in the heap. Positive values increase the heap size, while negative valuesdecrease it. <br><br><strong><em>void *sbrk(intptr_t increment);<\/em><\/strong><br><br><strong>increment<\/strong>: The number of bytes to change the data segment by. Positive values increase the heap, and negative values decrease it.<br><br><strong>mmap()<\/strong>: maps either a file or a block of memory into the address space of a process. It&#8217;s used for memory-mapped files and also for allocating memory, especially large blocks.<br><strong><em><br>void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);<\/em><\/strong><br><br><strong>addr<\/strong>: Suggested starting address for the mapping. Usually specified as NULL to let the kernel choose the starting address.<br><br><strong>length<\/strong>: The length of the mapping in bytes.<br><br><strong>prot<\/strong>: Desired memory protection (e.g., PROT_READ, PROT_WRITE).<br><br><strong>flags<\/strong>: Flags that determine how the mapping is applied. Common flags include MAP_PRIVATE, MAP_SHARED, and MAP_ANONYMOUS.<br><br><strong>fd<\/strong>: File descriptor of the file to be mapped. For anonymous memory mappings (i.e., not backed by a file), -1 is used.<br><br><strong>offset<\/strong>: Offset in the file where the mapping starts. For anonymous mappings, this is ignored.<br><br><strong>Returns<\/strong>: On success, mmap() returns a pointer to the starting address of the mapping. On failure, it returns MAP_FAILED, and errno is set appropriately.<br><br><br><br>LinkedIn Post: <a href=\"https:\/\/www.linkedin.com\/posts\/t-yashwanth-naidu_deep-dive-into-memory-with-yashpart-1-activity-7115098926747197440-rAGX\/?utm_source=share&amp;utm_medium=member_desktop\">https:\/\/www.linkedin.com\/posts\/t-yashwanth-naidu_deep-dive-into-memory-with-yashpart-1-activity-7115098926747197440-rAGX\/?utm_source=share&amp;utm_medium=member_desktop<\/a><\/p>\n\n\n\n<p><br><\/p>\n<\/div>\n<\/div>\n\n\n\n<p class=\"has-white-color has-text-color has-background has-link-color has-medium-font-size wp-elements-486e7799b35920eece0029316d5e98bd\" style=\"background:linear-gradient(135deg,rgb(35,23,11) 0%,rgb(254,45,45) 50%,rgb(107,0,62) 100%)\">~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br><strong>An Article by: <\/strong>Yashwanth Naidu Tikkisetty<br>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<\/p>\n\n\n\n<div class=\"wp-block-group is-vertical is-layout-flex wp-container-core-group-is-layout-8cf370e7 wp-block-group-is-layout-flex\">\n<div class=\"wp-block-group is-vertical is-layout-flex wp-container-core-group-is-layout-8cf370e7 wp-block-group-is-layout-flex\">\n<p><br><\/p>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Dynamic memory allocation functions like malloc(), calloc(), realloc(), and free() are crucial in programming. The improved version, dlmalloc(), is widely adopted due to its efficiency. When called, these functions trigger system calls like brk()\/sbrk() and mmap() to allocate memory. Understanding these processes is essential for efficient memory management in programming.<\/p>\n<a href=\"https:\/\/cthecosmos.com\/?p=4319\" class=\"more-link\">Read More <span class=\"screen-reader-text\">A Deep Dive Into Memory<\/span><\/a>","protected":false},"author":120055267,"featured_media":4329,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":false,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"_wpas_customize_per_network":false},"categories":[610,35264203,769114260,952411],"tags":[772321195,11240543,772321193,1827,88204,771201861,2201867,1471,100325149,11240544,1354040,34920936],"class_list":{"0":"post-4319","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","6":"hentry","7":"category-linux","8":"category-linux-3","9":"category-linuxlearnings","10":"category-short-articles","11":"tag-c-programming","12":"tag-calloc","13":"tag-embedded-systems","14":"tag-free","15":"tag-linux-fun","16":"tag-linuxlearning-2","17":"tag-malloc","18":"tag-memory","19":"tag-memory-allocators","20":"tag-realloc","21":"tag-short-article","22":"tag-ubuntu-2","24":"fallback-thumbnail"},"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/06\/image-1.png?fit=1024%2C768&ssl=1","jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8CiEf-17F","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/posts\/4319","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/users\/120055267"}],"replies":[{"embeddable":true,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4319"}],"version-history":[{"count":11,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/posts\/4319\/revisions"}],"predecessor-version":[{"id":4332,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/posts\/4319\/revisions\/4332"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/media\/4329"}],"wp:attachment":[{"href":"https:\/\/cthecosmos.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4319"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4319"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4319"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}