NET中的内存池实现

NET中的内存池实现

本文介绍了.NET中的内存池实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在.Net中实现内存池?

*要分配内存池.
*将接收味精并将该味精转储到该池中(所有味精大小均相同).
*如果没有使用任何味精(参考),则应将新的味精转储到那里.
*如果内存池已完全填满,则分配新的内存池.


简而言之
我想分配10000000字节的内存作为内存池.
我将以常规间隔分别获取400字节的味精(每个味精具有相同的大小).
我想将它一个接一个地存储到内存池1中.
如果没有使用任何味精(参考)(例如池中的第4个味精),则下一个新的味精将转到空位置(即第4个).
如果上述条件对您没有帮助
如果我们的内存池被完全填满(由于味精的数量),那么我想为新的味精分配新的内存池.

所有的消息都以字节为单位

How to implement Memory Pool in .Net?

* want to allocate Memory Pool.
* will recevie the msg and dump that msg into that pool(all msg size is same).
* if any msg is not in use(refrence over)then new msg should be dump there.
* if memory pool is completly fill then allocate new memory pool.


in Short
i want to allocate 10000000 bytes of memory as Memory pool.
i will get msg of 400 bytes each at regular inteval(each msg has same size).
i want to store it into memory pool 1 after another.
if any msg is not in use(refrence over)(eg 4th msg in the pool)then next new msg goes to empty location(ie 4th).
if above condition is not helping thn
if our memory pool is completely fill(due to number of msgs) then i want to allocate new memory pool for new msgs.

all the msgs are in bytes

推荐答案

public class MemoryPool : List<string>
{
    public string MaxSize { get; set; }

    public MemoryPool(int maxSize)
    {
        this.MaxSize = maxSize;
    }

    // add the new item, and delete the first item if the list exceeds the maximum specified size
    public new void Add(string item)
    {
        this.Add(item);
        if (this.Count > this.MaxSize)
        {
            this.RemoveAt(0);
        }
    }
}



进行较小的更改,就可以使列表更通用,可以接受ANY类型,同时仍保持最大长度.



With minor changes, you could make the list more generic, accepting ANY type, while still maintaining a maximum length.



这篇关于.NET中的内存池实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 14:54