Example #1
0
        /// <summary>
        /// 限制当前进程内存,
        /// 剩余内存限制(系统可用内存最低限制)
        /// 已使用内存限制(系统已用内存最高限制)
        /// </summary>
        /// <param name="avalidPhyMemoryMin">(当前操作系统)剩余内存限制(MB)</param>
        /// <param name="usedMemMax">(当前应用程序)已使用内存限制(MB)</param>
        public static void MemoryNeed(int avalidPhyMemoryMin, int usedMemMax)
        {
            uint avalidPhyMemory = MemoryHelper.GetAvalidPhyMemory();
            long usedMem         = MemoryHelper.WorkingSet64();

            if (avalidPhyMemory < avalidPhyMemoryMin * 1024 * 1024 ||
                usedMem > usedMemMax * 1024 * 1024)
            {
                throw new Exception(string.Format("当前系统资源不足以完成该操作,请释放部分资源后再试,剩余{0}<{1},已使用{2}<{3}。",
                                                  avalidPhyMemory / (1024f * 1024), avalidPhyMemoryMin, usedMem / (1024f * 1024), usedMemMax));
            }
        }
Example #2
0
        private void GetBlockNumber(Size size, out int blockXNum, out int blockYNum, out int blockWidth, out int blockHeight)
        {
            int w = size.Width;
            int h = size.Height;

            blockXNum   = 1;
            blockYNum   = 1;
            blockWidth  = w;
            blockHeight = h;
            int  MaxX         = 7000;
            int  MaxY         = 2000;
            uint mem          = MemoryHelper.GetAvalidPhyMemory(); //系统剩余内存
            long workingSet64 = MemoryHelper.WorkingSet64();       //为该进程已分配内存

            if (mem < 200 * 1024.0f * 1024)
            {
                throw new Exception("当前系统资源不足以完成该操作,请释放部分资源后再试。");
            }
            double usemem = mem;//;

#if !WIN64
            usemem = mem > 1800 * 1024.0f * 1024 - workingSet64 ? 1800 * 1024.0f * 1024 - workingSet64 : mem - workingSet64;
#endif
            MaxY = (int)(usemem / 100 / w);
            MaxY = MaxY < 2000 ? 2000 : MaxY;
            if (size.Width * size.Height <= MaxX * MaxY)
            {
                return;
            }
            while (blockWidth > MaxX)
            {
                blockXNum++;
                blockWidth = (int)Math.Floor((double)w / blockXNum);
            }
            while (blockHeight > MaxY)
            {
                blockYNum++;
                blockHeight = (int)Math.Floor((double)h / blockYNum);
            }
        }