<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          如何在C#中使用 ArrayPool,MemoryPool

          共 1095字,需瀏覽 3分鐘

           ·

          2022-01-20 10:46

          對資源的可復用是提升應用程序性能的一個非常重要的手段,比如本篇要分享的 ArrayPool 和 MemoryPool,它們就有效的減少了內(nèi)存使用和對GC的壓力,從而提升應用程序性能。

          什么是 ArrayPool

          System.Buffers 命名空間下提供了一個可對 array 進行復用的高性能池化類 ArrayPool,在經(jīng)常使用 array 的場景下可使用 ArrayPool 來減少內(nèi)存占用,它是一個抽象類,如下代碼所示:


          public?abstract?class?ArrayPool<T>
          {
          }

          可以想象一下你的業(yè)務場景中需要多次實例化 array,這么做有什么后果呢?很顯然每一次 new array 都會在托管堆上分配,同時當 array 不再使用時還需要 GC 去釋放,而 ArrayPool 就是為了解決此事而生的,它在池中動態(tài)維護若干個 array 對象,當你需要 new array 的時候只需從池中獲取即可。

          使用 ArrayPool

          可以通過下面三種方式來使用 ArrayPool 。

          • 通過 ArrayPool.Shared 屬性來獲取 ArrayPool 實例。

          • 通過 ArrayPool.Create() 來生成 ArrayPool 實例。

          • 通過繼承 ArrayPool 來生成一個自定義子類。

          下面的代碼展示了如何從 ArrayPool 中獲取一個 size >= 10 的 array 數(shù)組。


          var?shared?=?ArrayPool<int>.Shared;
          var?rentedArray?=?shared.Rent(10);

          上面的代碼一定要注意,雖然只租用了 10 個 size,但底層會返回 2的倍數(shù) ?的size , 也就是圖中的 2* 8 = 16。

          當什么時候不需要 rentedArray 了,記得再將它歸還到 ArrayPool 中,如下代碼所示。


          shared.Return(rentedArray);

          下面是僅供參考的完整代碼。


          ????????static?void?Main(string[]?args)
          ????????{
          ????????????var?shared?=?ArrayPool<int>.Shared;

          ????????????var?rentedArray?=?shared.Rent(10);

          ????????????for?(int?i?=?0;?i?10;?i++)
          ????????????{
          ????????????????rentedArray[i]?=?i?+?1;
          ????????????}

          ????????????for?(int?j?=?0;?j?10;?j++)
          ????????????{
          ????????????????Console.WriteLine(rentedArray[j]);
          ????????????}

          ????????????shared.Return(rentedArray);

          ????????????Console.ReadKey();
          ????????}

          創(chuàng)建自定義的 ArrayPool

          你也可以通過重寫 ArrayPool 來實現(xiàn)自定義的池化對象,如下代碼所示:


          ????public?class?CustomArrayPool<T>?:?ArrayPool<T>
          ????{
          ????????public?override?T[]?Rent(int?minimumLength)
          ????????{
          ????????????throw?new?NotImplementedException();
          ????????}
          ????????public?override?void?Return(T[]?array,?bool?clearArray?=?false)
          ????????{
          ????????????throw?new?NotImplementedException();
          ????????}
          ????}

          使用 MemoryPool

          System.Memory 命名空間下提供了一個內(nèi)存池對象 MemoryPool,在這之前你需要每次都 new 一個內(nèi)存塊出來,同時也增加了 GC 的負擔,有了 MemoryPool 之后,你需要的內(nèi)存塊直接從池中拿就可以了。


          ????????static?void?Main(string[]?args)
          ????????{

          ????????????var??memoryPool?=?MemoryPool<int>.Shared;

          ????????????var?rentedArray?=?memoryPool.Rent(10);

          ????????????for?(int?i?=?0;?i?10;?i++)
          ????????????{
          ????????????????rentedArray.Memory.Span[i]?=?i?+?1;
          ????????????}

          ????????????for?(int?j?=?0;?j?10;?j++)
          ????????????{
          ????????????????Console.WriteLine(rentedArray.Memory.Span[j]);
          ????????????}

          ????????????Console.ReadKey();
          ????????}

          ArrayPool vs MemoryPool

          從上面的演示可以看出, ArrayPool 是以 array 的形式向外租借,而 MemoryPool 則是以 內(nèi)存塊 的方式向外租借,所以在重復使用 array 的場景下可以優(yōu)選 ArrayPool 來提高性能,如果你的代碼是以 Memory 這種內(nèi)存塊的形式多次使用則優(yōu)先使用 MemoryPool

          譯文鏈接:https://www.infoworld.com/article/3596289/how-to-use-arraypool-and-memorypool-in-c.html


          瀏覽 76
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  91综合视频 | 亚洲黄色性爱 | 免费黄片视频 | 97视频网站 | 99精品国产91久久久久久无码 |