{"id":109970,"date":"2024-03-20T07:17:04","date_gmt":"2024-03-20T06:17:04","guid":{"rendered":"https:\/\/code-maze.com\/?p=109970"},"modified":"2024-04-02T11:44:26","modified_gmt":"2024-04-02T09:44:26","slug":"csharp-using-memory","status":"publish","type":"post","link":"https:\/\/code-maze.com\/csharp-using-memory\/","title":{"rendered":"Using Memory<T> For Efficient Memory Management in C#"},"content":{"rendered":"<p>Effective memory management is a crucial aspect of programming languages, especially when performance and efficiency are paramount. In C#, developers have access to a powerful API, Memory&lt;T&gt;, enabling them to work flexibly and efficiently with memory. In this article, we will delve deep into Memory&lt;T&gt;, exploring its features, advantages, ownership models, and practical usage scenarios.\u00a0<\/p>\n<div style=\"padding: 20px; border-left: 5px #dc2323 solid; display: block; margin-bottom: 20px; box-shadow: 1px 1px 5px 0px lightgrey;\">To download the source code for this article, you can visit our <a href=\"https:\/\/github.com\/CodeMazeBlog\/CodeMazeGuides\/tree\/main\/csharp-advanced-topics\/MemoryInCSharp\" target=\"_blank\" rel=\"nofollow noopener\">GitHub repository<\/a>.<\/div>\n<p>Let&#8217;s dive in.<\/p>\n<h2>Using Memory&lt;T&gt; in C#<\/h2>\n<p>We use a <a href=\"https:\/\/code-maze.com\/csharp-span-to-improve-application-performance\/\" target=\"_blank\" rel=\"noopener\">Span<\/a> to provide a memory-safe representation of a contiguous memory region. Like <code>Span&lt;T&gt;<\/code>, <code>Memory&lt;T&gt;<\/code> represents a contiguous memory region. <strong>However, it can reside on the managed heap as well as stack instead of just the stack like <code>Span<\/code><\/strong>. A <code>Span<\/code> is a <code>ref struct<\/code> stored in the stack with some limitations. The compiler will notify us if we incorrectly use a span or any other <code>ref struct<\/code>.<\/p>\n<p>Now that we have an understanding of <code>Memory&lt;T&gt;<\/code>, let us see how to create it:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">var numbers = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };\r\nvar memory = new Memory&lt;int&gt;(numbers);<\/pre>\n<p>We initialize an integer array, and from that array, we create a new instance of <code>Memory&lt;int&gt;<\/code> type.<\/p>\n<p>Now, let&#8217;s see other use cases of <code>Memory&lt;T&gt;<\/code>.<\/p>\n<h2>Allocate Memory&lt;T&gt; on Stack and Heap<\/h2>\n<p>We can allocate <code>Memory&lt;T&gt;<\/code>\u00a0on both the stack and the heap, unlike <code>Span&lt;T&gt;<\/code>, which is restricted to the stack:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public static void WorksWithBothStackAndHeap()\r\n{\r\n    Span&lt;int&gt; stackSpan = stackalloc int[3];\r\n    stackSpan[0] = 1;\r\n    stackSpan[1] = 2;\r\n    stackSpan[2] = 3;\r\n\r\n    var stackMemory = stackSpan.ToArray().AsMemory();\r\n    var heapArray = new[] { 4, 5, 6 };\r\n    var heapMemory = heapArray.AsMemory();\r\n\r\n    Console.WriteLine(\"Stack Memory:\");\r\n    foreach (var item in stackMemory.Span)\r\n    {\r\n        Console.WriteLine(item);\r\n    }\r\n\r\n    Console.WriteLine(\"\\nHeap Memory:\");\r\n    foreach (var item in heapMemory.Span)\r\n    {\r\n        Console.WriteLine(item);\r\n    }\r\n}<\/pre>\n<p>First, we create <code>stackSpan<\/code>, which is a <code>Span&lt;T&gt;<\/code> that is allocated on the stack using <code>stackalloc<\/code>.<\/p>\n<p>Then, we convert it to an array with <code>ToArray()<\/code> method and create a <code>Memory&lt;T&gt;<\/code> from it with <code>AsMemory()<\/code> method. This results in <code>stackMemory<\/code> being a <code>Memory&lt;T&gt;<\/code> that represents the same data as <code>stackSpan<\/code> but is allocated on the heap because arrays in .NET are always heap-allocated.<\/p>\n<p>We define <code>heapArray<\/code> as an array that we allocate on the heap, and create <code>heapMemory<\/code> using the\u00a0<code>AsMemory()<\/code> method. This results in <code>heapMemory<\/code> being a <code>Memory&lt;T&gt;<\/code> that represents the same data as <code>heapArray<\/code> and is also allocated on the heap.<\/p>\n<p>Finally, we display the contents of both <code>stackMemory<\/code> and <code>heapMemory<\/code>.<\/p>\n<h2>Memory&lt;T&gt; in C# With Async Methods<\/h2>\n<p>Now, let&#8217;s use <code>Memory&lt;T&gt;<\/code> in asynchronous code:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public static async Task ProcessMemoryAsync(Memory&lt;int&gt; memory)\r\n{\r\n    await Task.Delay(1000);\r\n    for (var index = 0; index &lt; memory.Span.Length; index++)\r\n    {\r\n        var item = memory.Span[index];\r\n\r\n        Console.WriteLine(item);\r\n    }\r\n}<\/pre>\n<p>Here, we simulate an asynchronous operation with <code>Task.Delay()<\/code>\u00a0and then we&#8217;re accessing the data in memory with <code>memory.Span<\/code> and printing it to the console. This would not be possible with <code>Span&lt;T&gt;<\/code> because we cannot use <code>Span&lt;T&gt;<\/code>\u00a0in asynchronous code due to being a <code>ref struct<\/code>.<\/p>\n<h2>AsMemory() Extension Method<\/h2>\n<p>The <strong>extension method <code>String.AsMemory()<\/code> allows us to create a <code>Memory&lt;char&gt;<\/code> object from a string without copying the underlying data<\/strong>. This can be useful when passing substrings to methods that accept <code>Memory&lt;T&gt;<\/code> parameters without incurring additional memory allocations:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public static void StringAsMemoryExtensionMethod()\r\n{\r\n    const string str = \"Hello Code Maze\";\r\n    var memory = str.AsMemory();\r\n    var slice = memory.Slice(6, 8);\r\n\r\n    Console.WriteLine(slice.ToString()); \/\/ \"Code Maze\"\r\n}\r\n<\/pre>\n<p>First, we create a <code>Memory&lt;char&gt;<\/code> from a string using the <code>AsMemory()<\/code> extension method. Then, we slice this <code>Memory&lt;char&gt;<\/code> to get a new <code>Memory&lt;char&gt;<\/code> representing a portion of the original string. Finally, we display this slice by converting it to a string using the <code>ToString()<\/code> method.<\/p>\n<p>Next, let&#8217;s focus on advanced techniques in memory management.<\/p>\n<h2>Ownership Models and IMemoryOwner Interface<\/h2>\n<p>The <code>MemoryPool&lt;T&gt;.Rent()<\/code> method returns the <code>IMemoryOwner&lt;T&gt;<\/code> interface, which acts as an owner of a memory block. The shared pool allows for the renting of the memory block. When the memory is no longer in use, the block&#8217;s owner is responsible for disposing of it. Let&#8217;s see this with the example of how to use <code>IMemoryOwner&lt;T&gt;<\/code> and <code>MemoryPool&lt;T&gt;<\/code>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public static void UseMemoryOwner()\r\n{\r\n    using IMemoryOwner&lt;int&gt; owner = MemoryPool&lt;int&gt;.Shared.Rent(10);\r\n    var memory = owner.Memory;\r\n\r\n    for (var i = 0; i &lt; memory.Length; i++)\r\n    {\r\n        memory.Span[i] = i;\r\n    }\r\n    foreach (var item in memory.Span)\r\n    {\r\n        Console.WriteLine(item);\r\n    }\r\n}<\/pre>\n<p>First, we rent a memory block from the shared pool with <code>MemoryPool&lt;int&gt;.Shared.Rent()<\/code> method. This returns an<code> IMemoryOwner&lt;int&gt;<\/code> that owns the rented block of memory.<\/p>\n<p>Next, we retrieve a <code>Memory&lt;int&gt;<\/code> that represents this memory block with the <code>owner.Memory<\/code> property. We use this <code>Memory&lt;int&gt;<\/code> to store some data, and then display this data.<\/p>\n<p>Finally, we dispose of the <code>IMemoryOwner&lt;int&gt;<\/code> with the using statement. <strong>This returns the block of memory to the pool, making it available for subsequent <code>Rent()<\/code> calls.<\/strong><\/p>\n<p>Now that we have a comprehensive understanding of <code>Memory&lt;T&gt;<\/code>, let&#8217;s see a real-world use case of file I\/O operations using <code>Memory&lt;T&gt;<\/code>.<\/p>\n<h2>Practical Scenario For Using Memory&lt;T&gt; in C#<\/h2>\n<p>We can use <code>MemoryPool&lt;T&gt;<\/code>, <code>IMemoryOwner&lt;T&gt;<\/code>, and the <code>IMemoryOwner.Memory<\/code> property in a practical scenario.<\/p>\n<p>In this case, we&#8217;ll create a method that reads data from a file into a rented block of memory, processes the data, and then returns the memory to the pool:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public static async Task ProcessFileAsync(string filePath)\r\n{\r\n    using var owner = MemoryPool&lt;byte&gt;.Shared.Rent(4096);\r\n    Memory&lt;byte&gt; buffer = owner.Memory.Slice(0, 4096);\r\n    await using FileStream stream = File.OpenRead(filePath);\r\n\r\n    int bytesRead;\r\n    while ((bytesRead = await stream.ReadAsync(buffer)) &gt; 0)\r\n    {\r\n        var data = buffer.Slice(0, bytesRead);\r\n        for (var index = 0; index &lt; data.Span.Length; index++)\r\n        {\r\n            var b = data.Span[index];\r\n            Console.Write((char)b);\r\n        }\r\n\r\n        Console.WriteLine();\r\n    }\r\n}<\/pre>\n<p>To begin, in the <code>ProcessFileAsync()<\/code> method, we obtain a block of memory from the shared pool by calling the <code>MemoryPool&lt;byte&gt;.Shared.Rent()<\/code> method. This method returns an\u00a0<code>IMemoryOwner&lt;byte&gt;<\/code> interface that owns the block of memory.<\/p>\n<p>Consequently, we retrieve a <code>Memory&lt;byte&gt;<\/code> object that represents the block of memory using the <code>owner.Memory<\/code> property. Then, we use this <code>Memory&lt;byte&gt;<\/code> object as a buffer to read data from a file with the <code>FileStream.ReadAsync()<\/code> method. Once we have read the data, we process it by printing it to the console.<\/p>\n<p>Finally, we dispose of the <code>IMemoryOwner&lt;byte&gt;<\/code> interface with the using statement in the <code>ProcessFileAsync()<\/code> method. This action returns the block of memory to the pool, making it available for subsequent <code>Rent()<\/code> calls.<\/p>\n<h2>Conclusion<\/h2>\n<p>Developers prioritizing performance and efficiency in their applications can benefit significantly from Memory&lt;T&gt;, a flexible and robust API for managing memory in C#. With the ability to be allocated on both the stack and the heap, interoperability with strings, and ownership models, Memory&lt;T&gt; is a valuable asset. By understanding and leveraging Memory&lt;T&gt;, C# developers can optimize memory usage, improve application performance, and build more robust and scalable software solutions.\u00a0<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Effective memory management is a crucial aspect of programming languages, especially when performance and efficiency are paramount. In C#, developers have access to a powerful API, Memory&lt;T&gt;, enabling them to work flexibly and efficiently with memory. In this article, we will delve deep into Memory&lt;T&gt;, exploring its features, advantages, ownership models, and practical usage scenarios.\u00a0 [&hellip;]<\/p>\n","protected":false},"author":96,"featured_media":62189,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[12,2134],"tags":[10,1811,2132,2133,1318,1320],"class_list":["post-109970","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-csharp","category-performance","tag-net","tag-c","tag-memory","tag-memory-management","tag-performance-in-c","tag-span","et-has-post-format-content","et_post_format-et-post-format-standard"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Using Memory For Efficient Memory Management in C#<\/title>\n<meta name=\"description\" content=\"This article showcases using Memory in C# instead of Span to overcome some of its limitations, as well as performance benchmarks\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/code-maze.com\/csharp-using-memory\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Using Memory For Efficient Memory Management in C#\" \/>\n<meta property=\"og:description\" content=\"This article showcases using Memory in C# instead of Span to overcome some of its limitations, as well as performance benchmarks\" \/>\n<meta property=\"og:url\" content=\"https:\/\/code-maze.com\/csharp-using-memory\/\" \/>\n<meta property=\"og:site_name\" content=\"Code Maze\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-20T06:17:04+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-02T09:44:26+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1100\" \/>\n\t<meta property=\"og:image:height\" content=\"620\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Muhammad Afzal Qureshi\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@CodeMazeBlog\" \/>\n<meta name=\"twitter:site\" content=\"@CodeMazeBlog\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Muhammad Afzal Qureshi\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":[\"Article\",\"BlogPosting\"],\"@id\":\"https:\/\/code-maze.com\/csharp-using-memory\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/csharp-using-memory\/\"},\"author\":{\"name\":\"Muhammad Afzal Qureshi\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/203959dc704ebf0d0bcf98d6576c63dc\"},\"headline\":\"Using Memory For Efficient Memory Management in C#\",\"datePublished\":\"2024-03-20T06:17:04+00:00\",\"dateModified\":\"2024-04-02T09:44:26+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/code-maze.com\/csharp-using-memory\/\"},\"wordCount\":844,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/csharp-using-memory\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"keywords\":[\".NET\",\"C#\",\"Memory\",\"Memory Management\",\"Performance in C#\",\"Span\"],\"articleSection\":[\"C#\",\"Performance\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/code-maze.com\/csharp-using-memory\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/code-maze.com\/csharp-using-memory\/\",\"url\":\"https:\/\/code-maze.com\/csharp-using-memory\/\",\"name\":\"Using Memory For Efficient Memory Management in C#\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/code-maze.com\/csharp-using-memory\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/csharp-using-memory\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"datePublished\":\"2024-03-20T06:17:04+00:00\",\"dateModified\":\"2024-04-02T09:44:26+00:00\",\"description\":\"This article showcases using Memory in C# instead of Span to overcome some of its limitations, as well as performance benchmarks\",\"breadcrumb\":{\"@id\":\"https:\/\/code-maze.com\/csharp-using-memory\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/code-maze.com\/csharp-using-memory\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/csharp-using-memory\/#primaryimage\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"width\":1100,\"height\":620,\"caption\":\"C# Development\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/code-maze.com\/csharp-using-memory\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/code-maze.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Using Memory For Efficient Memory Management in C#\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/code-maze.com\/#website\",\"url\":\"https:\/\/code-maze.com\/\",\"name\":\"Code Maze\",\"description\":\"Learn. Code. Succeed.\",\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/code-maze.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/code-maze.com\/#organization\",\"name\":\"Code Maze\",\"url\":\"https:\/\/code-maze.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"width\":3511,\"height\":3510,\"caption\":\"Code Maze\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/CodeMazeBlog\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/203959dc704ebf0d0bcf98d6576c63dc\",\"name\":\"Muhammad Afzal Qureshi\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2023\/12\/Afzal-Profile-Pic-150x150.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2023\/12\/Afzal-Profile-Pic-150x150.png\",\"caption\":\"Muhammad Afzal Qureshi\"},\"description\":\"I have more than 20 years of experience in the industry, specializing in Software Development Life Cycle (SDLC), Strategic IT Planning, and Solution Architecture across diverse sectors. I am proficient in Object-Oriented and Functional Programming, Design Patterns, and Cloud Native based SaaS Solutions with cloud providers like Microsoft Azure and AWS. I lead teams to align deliverables precisely and have a track record demonstrating a comprehensive understanding of SDLC, industry-standard development, and championing the adoption of Microservices Architecture using various technology stacks like .NET, NodeJS, Python, and Go.\",\"sameAs\":[\"https:\/\/www.linkedin.com\/in\/goldytech\/\"],\"url\":\"https:\/\/code-maze.com\/author\/muhammadafzal-qureshi\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Using Memory For Efficient Memory Management in C#","description":"This article showcases using Memory in C# instead of Span to overcome some of its limitations, as well as performance benchmarks","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/code-maze.com\/csharp-using-memory\/","og_locale":"en_US","og_type":"article","og_title":"Using Memory For Efficient Memory Management in C#","og_description":"This article showcases using Memory in C# instead of Span to overcome some of its limitations, as well as performance benchmarks","og_url":"https:\/\/code-maze.com\/csharp-using-memory\/","og_site_name":"Code Maze","article_published_time":"2024-03-20T06:17:04+00:00","article_modified_time":"2024-04-02T09:44:26+00:00","og_image":[{"width":1100,"height":620,"url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","type":"image\/png"}],"author":"Muhammad Afzal Qureshi","twitter_card":"summary_large_image","twitter_creator":"@CodeMazeBlog","twitter_site":"@CodeMazeBlog","twitter_misc":{"Written by":"Muhammad Afzal Qureshi","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["Article","BlogPosting"],"@id":"https:\/\/code-maze.com\/csharp-using-memory\/#article","isPartOf":{"@id":"https:\/\/code-maze.com\/csharp-using-memory\/"},"author":{"name":"Muhammad Afzal Qureshi","@id":"https:\/\/code-maze.com\/#\/schema\/person\/203959dc704ebf0d0bcf98d6576c63dc"},"headline":"Using Memory For Efficient Memory Management in C#","datePublished":"2024-03-20T06:17:04+00:00","dateModified":"2024-04-02T09:44:26+00:00","mainEntityOfPage":{"@id":"https:\/\/code-maze.com\/csharp-using-memory\/"},"wordCount":844,"commentCount":0,"publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"image":{"@id":"https:\/\/code-maze.com\/csharp-using-memory\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","keywords":[".NET","C#","Memory","Memory Management","Performance in C#","Span"],"articleSection":["C#","Performance"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/code-maze.com\/csharp-using-memory\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/code-maze.com\/csharp-using-memory\/","url":"https:\/\/code-maze.com\/csharp-using-memory\/","name":"Using Memory For Efficient Memory Management in C#","isPartOf":{"@id":"https:\/\/code-maze.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/code-maze.com\/csharp-using-memory\/#primaryimage"},"image":{"@id":"https:\/\/code-maze.com\/csharp-using-memory\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","datePublished":"2024-03-20T06:17:04+00:00","dateModified":"2024-04-02T09:44:26+00:00","description":"This article showcases using Memory in C# instead of Span to overcome some of its limitations, as well as performance benchmarks","breadcrumb":{"@id":"https:\/\/code-maze.com\/csharp-using-memory\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/code-maze.com\/csharp-using-memory\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/csharp-using-memory\/#primaryimage","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","width":1100,"height":620,"caption":"C# Development"},{"@type":"BreadcrumbList","@id":"https:\/\/code-maze.com\/csharp-using-memory\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/code-maze.com\/"},{"@type":"ListItem","position":2,"name":"Using Memory For Efficient Memory Management in C#"}]},{"@type":"WebSite","@id":"https:\/\/code-maze.com\/#website","url":"https:\/\/code-maze.com\/","name":"Code Maze","description":"Learn. Code. Succeed.","publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/code-maze.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/code-maze.com\/#organization","name":"Code Maze","url":"https:\/\/code-maze.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","width":3511,"height":3510,"caption":"Code Maze"},"image":{"@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/CodeMazeBlog"]},{"@type":"Person","@id":"https:\/\/code-maze.com\/#\/schema\/person\/203959dc704ebf0d0bcf98d6576c63dc","name":"Muhammad Afzal Qureshi","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/person\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2023\/12\/Afzal-Profile-Pic-150x150.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2023\/12\/Afzal-Profile-Pic-150x150.png","caption":"Muhammad Afzal Qureshi"},"description":"I have more than 20 years of experience in the industry, specializing in Software Development Life Cycle (SDLC), Strategic IT Planning, and Solution Architecture across diverse sectors. I am proficient in Object-Oriented and Functional Programming, Design Patterns, and Cloud Native based SaaS Solutions with cloud providers like Microsoft Azure and AWS. I lead teams to align deliverables precisely and have a track record demonstrating a comprehensive understanding of SDLC, industry-standard development, and championing the adoption of Microservices Architecture using various technology stacks like .NET, NodeJS, Python, and Go.","sameAs":["https:\/\/www.linkedin.com\/in\/goldytech\/"],"url":"https:\/\/code-maze.com\/author\/muhammadafzal-qureshi\/"}]}},"_links":{"self":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/109970","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/users\/96"}],"replies":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/comments?post=109970"}],"version-history":[{"count":4,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/109970\/revisions"}],"predecessor-version":[{"id":110002,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/109970\/revisions\/110002"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media\/62189"}],"wp:attachment":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media?parent=109970"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/categories?post=109970"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/tags?post=109970"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}