.NET中的内存池实现 [英] Memory Pool implementation in .Net

查看:125
本文介绍了.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

推荐答案

内存池?从技术上讲,所有变量都是内存池".由于您显然要存储字符串,所以我认为您真正想要的是可以管理其自身长度的通用List.像这样的东西:

A memory pool? Technically, all variables are "memory pools". Since you''re apparently storing strings, what I think you really want is a generic List that can manage its own length. Something like this:

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.


您在用错误的术语来考虑.您是对内存池"的描述,以及如何使用它来描述代表您的消息并将这些消息对象存储在通用List对象List< t>()中的类.您完全不必担心管理内存.
You''re thinking about this in the wrong terms. You''re description of a "memory pool" and how you want to use it describes a class that represents your message and storing those message objects in a generic List object, List<t>(). You don''t have to worry about managing the memory at all.


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆