示例#1
0
        /// <summary>
        /// 缓存对象数量快超出一级缓存时,对其进行扩容
        /// </summary>
        private void try_fix_pool()
        {
            int free_cache_count = get_free_cache_count();
            int cache_count      = get_cache_count();

            // 数量超过一级缓存的90%时
            if ((((float)cache_count) / ((float)mCacheBlockMaxCount)) >= 0.9f)
            {
                // 当前内存池中可用对象的比例少于20%时,需要进行扩容
                if ((((float)free_cache_count) / ((float)cache_count)) < 0.2f)
                {
                    mCacheBlockMaxCount += 32;
                    if (mCacheBlockMaxCount > mBlockLimitCount)
                    {
                        mCacheBlockMaxCount = mBlockLimitCount;
                    }
                }
                else
                {
                    // 销毁当前内存池中未被使用的对象,销毁数量为50%
                    for (int i = 0; i < mPool.Count; i++)
                    {
                        CachedObjectInfo info = mPool[i];
                        int num = info.get_free_cache_count();
                        info.shrink(num / 2);
                    }
                }
            }
        }
示例#2
0
        /// <summary>
        /// 定时检查标记为可销毁的对象
        /// </summary>
        public void update()
        {
            float time = Time.time;

            if ((time - mLastUpdateTime) >= mUpdateFrequency)
            {
                mLastUpdateTime = time;
                if (mPool != null)
                {
                    // 每次随机获取一个CacheObjectInfo
                    int index = UnityEngine.Random.Range(0, mPool.Count);
                    if ((index >= 0) && (index < mPool.Count))
                    {
                        CachedObjectInfo info = mPool[index];
                        if (info == null)
                        {
                            mPool.RemoveAt(index);
                        }
                        else
                        {
                            // 根据标记可销毁的时间间隔来计算需要销毁的对象数量
                            float num = Time.time - info.get_weight();
                            int   max_shrink_count = (int)(num / mWornTime);
                            if (max_shrink_count > 0)
                            {
                                int free_cache_count = info.get_free_cache_count();
                                if (free_cache_count <= 0)// 无可用缓存对象时,移除CacheObjectInfo
                                {
                                    mPool.RemoveAt(index);
                                }
                                else
                                {
                                    // 移除的数量不能超出当前可用缓存对象
                                    max_shrink_count = (max_shrink_count <= free_cache_count) ? max_shrink_count : free_cache_count;
                                    info.shrink(max_shrink_count);
                                }
                            }
                        }
                    }
                }
            }
        }