01.Blogs :
CCTING  

Optimize your System Memory using C#

 

I try to use TuneUp2006 to optimize my memory, but it takes too long and my hard disk keep making sounds. I hate waiting for optimization because it keep me out of my work as i am using my old Celeron 2.2GHz notebook. Hence i wrote a simple program to optimize my notebook memory. You don't have to think of complex algorithm to free memory, and what you need to do is JUST TO FORCE WINDOWS to FREE UP more memory for your system. Therefore, what you need to do is to consume more physical memory :D. Write a program like below. Run it, and terminate it by force after a few seconds, and your system memory will be recovered to 30-80%. It is much faster compared to TuneUp memory Optimization. Have Fun

 

Author: Ting Choo Chiaw

email: choochiaw.ting@gmail.com

 

 [StructLayout(LayoutKind.Sequential)]
        internal class MEMORYSTATUS
        {
            internal int length;
            internal int memoryLoad;
            internal uint totalPhys;
            internal uint availPhys;
            internal uint totalPageFile;
            internal uint availPageFile;
            internal uint totalVirtual;
            internal uint availVirtual;
        }

        [DllImport("kernel32.dll", SetLastError = true)]
        internal static extern bool GlobalMemoryStatus( MEMORYSTATUS buffer);

        public void FreeMem()
        {
            MEMORYSTATUS status = new MEMORYSTATUS();

            GlobalMemoryStatus(status);

           
            bool bContinue = false;
            int memory = (int)status.totalPhys;
            int iCount = 0;
            do
            {
               
                try
                {
                    byte[] hhh = new byte[memory];

                    using (System.IO.MemoryStream ms = new MemoryStream(memory))
                    {
                        ms.Write(hhh, 0, memory);
                        ms.Close();
                        iCount++;
                        memory = (int)(memory * 1.1);

                        if(iCount >10) //you can increase / decrease. smaller value = faster.
                            bContinue = false;
                    }

                }
                catch (System.OutOfMemoryException)
                {
                    memory = (int)(memory * 0.99);
                    bContinue = true;
                }
                finally
                {
                    System.GC.AddMemoryPressure((int)status.totalPhys);
                    System.GC.GetGeneration(System.GC.MaxGeneration);
                    System.GC.Collect();
                }
            } while (bContinue);
        }


        private void btnFreeMem_Click(object sender, EventArgs e)
        {
            System.Threading.Thread a =    new System.Threading.Thread(new System.Threading.ThreadStart(FreeMem));
            a.IsBackground = true;
            a.Start();

            a.Join(1000); // the more u put, the better result it is
            a.Abort();
            Application.Exit();
           
        }

posted on Friday, December 01, 2006 9:23 AM by CCTING


 
03.UPDATE CALENDAR :
<December 2006>
SunMonTueWedThuFriSat
262728293012
3456789
10111213141516
17181920212223
24252627282930
31123456

05.MY LINKS :

07.Subscriptions :

Subscriptions


© Copyright 2005 Microsoft Corporation. All Rights Reserved.
Terms of Use | Privacy Statement | Code of Conduct | Hosted by MaximumASP for Microsoft
WHO-BAR