?????????????????????в???????????new??free?????????????????????????
????????????????????
public abstract class ObjectBase
{
public abstract void Init(params object[] paramList);
}
//??????????(???????洢???????????????????????????????????????????????????)
public class ObjectPoolManager<T> where T : ObjectBase?? new()
{
private static ObjectPoolManager<T> instance = null;
private int blockCapacity = 1000;
private static object doubleCheckLock = new object();
private static object objLock = new object();
private bool inited = false;
private ConcurrentDictionary<string?? Stack<T>> objectPool = new ConcurrentDictionary<string?? Stack<T>>();
private ObjectPoolManager() { }
public static ObjectPoolManager<T> Instance
{
get
{
if (instance == null)
{
lock (doubleCheckLock)
{
if (instance == null)
{
instance = new ObjectPoolManager<T>();
}
}
}
return instance;
}
}
//??????????
public string Init(int blockCapacity)
{
lock (objLock)
{
try
{
if (blockCapacity < 1 || blockCapacity > int.MaxValue)
{
this.blockCapacity = 1000;
}
else
{
this.blockCapacity = blockCapacity;
}
Stack<T> freeObjList = new Stack<T>();
for (int index = 0; index < blockCapacity; index++)
{
T obj = new T();
freeObjList.Push(obj);
}
objectPool[typeof(T).ToString()] = freeObjList;
inited = true;
return null;
}
catch (Exception ex)
{
return typeof(T).ToString() + " -> ExceptionMessage:" + ex.Message + (ex.InnerException != null ? ("InnerExceptionMessage:" + ex.InnerException.Message) : "");
}
}
}
//??????
public T NewObject(params object [] paramList)
{
lock (objLock)
{
try
{
if (!inited)
{
Init(blockCapacity);
}
string key = typeof(T).ToString();
Stack<T> objList = objectPool[key];
if (objList.Count > 0)
{
T obj = objList.Pop();
obj.Init(paramList);
return obj;
}
else
{
for (int index = 0; index < this.blockCapacity; index++)
{
T newObj = new T();
objList.Push(newObj);
}
T obj = objList.Pop();
obj.Init(paramList);
return obj;
}
}
catch (Exception ex)
{
//ServerUtil.RecordLog(LogType.Error?? ex);
T newObj = new T();
newObj.Init(paramList);
return newObj;
}
}
}
//??????
public void FreeObject(T obj)
{
lock (objLock)
{
try
{
if (obj == default(T)) return;
Stack<T> objList = objectPool[typeof(T).ToString()];
if (!objList.Contains(obj))
{
objList.Push(obj);
}
}
catch (Exception ex)
{
//ServerUtil.RecordLog(LogType.Error?? ex);
}
}
}
}
??????????????ó??????????????
??????????????????????
//??÷???????????β
public sealed class CodeElapseChecker
{
public static void Initialize()
{
Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
Thread.CurrentThread.Priority = ThreadPriority.Highest;
Time("??????????????????????????"?? 1?? () => { });
}
public static void Time(string name?? Action action?? int iterationCnt = 1)
{
Time(name?? iterationCnt?? action);
}
public static void Time(string name?? int iteration?? Action action)
{
// 1.
ConsoleColor currentForeColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(name);
// 2.
GC.Collect(GC.MaxGeneration?? GCCollectionMode.Forced);
int[] gcCounts = new int[GC.MaxGeneration + 1];
for (int i = 0; i <= GC.MaxGeneration; i++)
{
gcCounts[i] = GC.CollectionCount(i);
}
// 3.
Stopwatch watch = new Stopwatch();
watch.Start();
long cycleCount = GetCycleCount();
for (int i = 0; i < iteration; i++) action();
long cpuCycles = GetCycleCount() - cycleCount;
watch.Stop();
// 4.
Console.ForegroundColor = currentForeColor;
Console.WriteLine(" Time Elapsed: " + watch.ElapsedMilliseconds.ToString("N0") + "ms");
Console.WriteLine(" CPU Cycles: " + cpuCycles.ToString("N0"));
// 5.
for (int i = 0; i <= GC.MaxGeneration; i++)
{
int count = GC.CollectionCount(i) - gcCounts[i];
Console.WriteLine(" Gen " + i + ": " + count);
}
Console.WriteLine();
}
private static long GetCycleCount()
{
//ulong cycleCount = 0;
//QueryThreadCycleTime(GetCurrentThread()?? ref cycleCount);
//return cycleCount;
return GetCurrentThreadTimes();
}
[DllImport("kernel32.dll"?? SetLastError = true)]
static extern bool GetThreadTimes(IntPtr hThread?? out long lpCreationTime??
out long lpExitTime?? out long lpKernelTime?? out long lpUserTime);
private static long GetCurrentThreadTimes()
{
long l;
long kernelTime?? userTimer;
GetThreadTimes(GetCurrentThread()?? out l?? out l?? out kernelTime??
out userTimer);
return kernelTime + userTimer;
}
//[DllImport("kernel32.dll")]
//[return: MarshalAs(UnmanagedType.Bool)]
//static extern bool QueryThreadCycleTime(IntPtr threadHandle?? ref ulong cycleTime);
[DllImport("kernel32.dll")]
static extern IntPtr GetCurrentThread();
}