01.Blogs :
CCTING  
Optimize your System Memory using C#
Friday, December 01, 2006 9:23 AM
 

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();
           
        }

0 Comments | Post a Comment |

posted  by  CCTING  with 

External IP + C# + Router IP
Friday, December 01, 2006 9:16 AM
There are many ways of detecting external IP of router. One of the easiest if using "web services".
However, this method has risk of using because the web service may change anytime and therefore this method will no more applicable.
I will talk about how to request external IP from "external DNS" in other time. Today i talk about the easiest way first.
System.Net.HttpWebRequest request =
(System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create("http://www.what
ismyip.org/");

request.UserAgent = "User-Agent: Mozilla/4.0 (compatible; MSIE
6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
System.Net.HttpWebResponse response =
	(System.Net.HttpWebResponse)request.GetResponse();

string myExternalIP = string.Empty;

using (System.IO.StreamReader reader = new
	StreamReader(response.GetResponseStream()))
{

	myExternalIP = reader.ReadToEnd();
	reader.Close();
}

response.Close();
MessageBox.Show(myExternalIP);

0 Comments | Post a Comment |

Rated Average [3 out of 5].

posted  by  CCTING  with 

Cheapest MSDN subscription for Developers in Malaysia
Sunday, November 13, 2005 11:22 AM

May i know how can a company gets a Cheapest MSDN subscription for Developers in Malaysia?

I remember a company can subscribe to MSDN around RM1k after discount. But i am not sure which package is that.. anyone?

 

0 Comments | Post a Comment |

posted  by  CCTING  with 

Haze.NET
Friday, August 12, 2005 3:07 PM
West Malaysia suffer from haze...http://www.jas.sains.my/jas/API.htm

Haze.NET is nothing to do with Microsoft .NET, pls dun misunderstand... it is the network of Malaysia Haze Monitor.....

| |

posted  by  CCTING  (Comments Off) 

PROBLEMATIC SPLIT - WHIDBEY
Tuesday, August 09, 2005 12:22 PM

string AString = "Ting, Choo";
string
[] tokens = AString.Split(new char[] { ',' });
MessegeBox.Show(takens[0]);  //==>NO PROBLEM
MessegeBox.Show(takens[1]); // ==>ERROR : out of index..

This is very ignoring, to overcome this error for Whidbey Beta 2,
instead of using tokens[1], i did this
   string token2 = AString.Replace(tokens[0]);

As i explore more in Whidbey 2, i have found countless of errors / mistakes which draw me to "uneffective" solutions..
What say u?

:(( bad bad bad.. writing a article for conference paper not an easy task for me, how about u.. give me many hands??? or even laser printers :D hahahaha..

| |

posted  by  CCTING  (Comments Off) 

Can't login to thespoke.net
Monday, June 20, 2005 5:30 AM
Steps:
1) IE->Tools->Internet Options->Privacy->Remove all
2) Disable Google toolbar->autofil option.


:D.. finally can login to thespoke.net again..

| |

posted  by  CCTING  (Comments Off) 

Microsoft Virtual Lab for .net learning
Friday, June 10, 2005 5:55 AM
http://g.msn.com/0AD0001K/723807.1??PID=2584906&UIT=G&TargetID=1063753&AN=16271&PG=CMSHP2

Seems like it does not support Microsoft password single-login compatibility..

:((

| |

posted  by  CCTING  (Comments Off) 

WSRF for .NET
Thursday, June 09, 2005 12:06 PM
Wow... WSRF for .NET.. excellent..!
http://www.cs.virginia.edu/~humphrey/papers/WSRFComparison2005.pdf
This research is supported by MICROSOFT>... i am so outofdated!!!!

| |

posted  by  CCTING  (Comments Off) 

The Open Source Development Environment for .NET
Wednesday, June 08, 2005 8:18 AM
http://www.icsharpcode.net/OpenSource/SD/
hahaha now i will never make use of VS2005 liao.. this ide only 7MB...

| |

posted  by  CCTING  (Comments Off) 

VS 2005 out this november?
Wednesday, June 08, 2005 4:54 AM
is that true?

| |

posted  by  CCTING  (Comments Off) 

split and join Files
Tuesday, June 07, 2005 5:59 AM

public class FilePartitionManager

{

private FilePartitionManager()

{

}

private static string GetFolder(string FileName)

{

System.IO.FileInfo info = new FileInfo(FileName);

return info.DirectoryName;

}

public static void WritePartition(int PartitionLength, string ReadFileName, string WriteFileName)

{

FileStream reader = new FileStream(ReadFileName, FileMode.Open, FileAccess.Read);

byte[] readBytes = new byte[PartitionLength];

string directory = GetFolder(WriteFileName);

int iTotalPartition = CountPartition(reader.Length, PartitionLength);

if (!System.IO.Directory.Exists(directory))

System.IO.Directory.CreateDirectory(directory);

for (int i = 0; i < iTotalPartition; i++)

{

string FileName = WriteFileName + "." + i.ToString() + "." + iTotalPartition.ToString();

FileStream writer = new FileStream(FileName, FileMode.Create, FileAccess.Write);

readBytes.Initialize();

int iRead = reader.Read(readBytes, 0, PartitionLength);

writer.Write(readBytes, 0, iRead);

writer.Close();

}

reader.Close();

GC.Collect();

}

public static void JoinPartition(string OneOfPartitionFileName)

{

string WriteFileName = GetOrginalFileName(OneOfPartitionFileName);

int iTotalPartition = CountPartition(OneOfPartitionFileName);

FileStream writer = new FileStream(WriteFileName, FileMode.Create, FileAccess.Write);

for (int i = 0; i < iTotalPartition; i++)

{

string PartitionFileName = string.Format("{0}.{1}.{2}", WriteFileName, i, iTotalPartition);

FileStream reader = new FileStream(PartitionFileName, FileMode.Open, FileAccess.Read);

byte[] readBytes = new byte[reader.Length];

readBytes.Initialize();

int iRead = reader.Read(readBytes, 0, readBytes.Length);

writer.Write(readBytes, 0, iRead);

reader.Close();

}

writer.Close();

GC.Collect();

}

public static void JoinDeletePartition(string OneOfPartitionFileName)

{

string WriteFileName = GetOrginalFileName(OneOfPartitionFileName);

int iTotalPartition = CountPartition(OneOfPartitionFileName);

FileStream writer = new FileStream(WriteFileName, FileMode.Create, FileAccess.Write);

for (int i = 0; i < iTotalPartition; i++)

{

string PartitionFileName = string.Format("{0}.{1}.{2}", WriteFileName, i, iTotalPartition);

FileStream reader = new FileStream(PartitionFileName, FileMode.Open, FileAccess.Read);

byte[] readBytes = new byte[reader.Length];

readBytes.Initialize();

int iRead = reader.Read(readBytes, 0, readBytes.Length);

writer.Write(readBytes, 0, iRead);

reader.Close();

System.IO.File.Delete(PartitionFileName);

GC.Collect();

}

writer.Close();

GC.Collect();

}

 

private static string GetOrginalFileName(string OneOfPartitionFileName)

{

int iPartition = CountPartition(OneOfPartitionFileName);

string []names = OneOfPartitionFileName.Split('.');

int namesLength = names.Length;

string originalName = "";

for (int i = 0; i < namesLength - 2; i++)

originalName += names[i]+".";

originalName = originalName.Substring(0, originalName.Length - 1);

return originalName;

}

private static int CountPartition(long iTotalLength, int PartitionLength)

{

double aa = (double)iTotalLength;

double bb = (double)PartitionLength;

double cc = aa / bb;

int c = (int)cc;

double result = cc - c;

if (result > 0)

return (c + 1);

return c;

}

private static int CountPartition(string PartitionFileName)

{

string[] names = PartitionFileName.Split('.');

bool bValid = true;

foreach (char c in names[names.Length - 1])

if (!char.IsDigit(c))

bValid = false;

if (!bValid)

{

System.Windows.Forms.MessageBox.Show(PartitionFileName + " is not a valid partition file");

return 0;

}

return Convert.ToInt32( names[names.Length - 1]);

}

 

}

| |

posted  by  CCTING  (Comments Off) 

Unbelievable Performance - Unsafe Image Comparing
Monday, June 06, 2005 12:06 PM
Approach one: Safe approach to compare two images

Two approaches of comparing two same size bitmap (Assume all pixel size as 4 = PNG (RGBA).
R=REd
G= GREEN
B=BLUE
A=Alpha (Transparency)
so the pixel size = 4..
(i not sure whether this is true, but it does work !!, i hope those who knows give some comment)
The result:
Safe approach very slow,, 1 comparison / 1000 ms almost take my CPU utilizatin 100%
Unsafe approach very fast, 1 comparison / 1000 ms almost take my CPU utilization less than 1%

10 comparison / 1000ms with unsafe ==> 20% CPU utilization.

So.. hahahaha nice right unsafe thing.

Here my source code.. have fun

Approach ONE: Safe approach
public
static double SafeCompare(ref Bitmap bmp1,ref Bitmap bmp2)
{
   int iMismatch = 0;
   for (int x = 0; x < bmp1.Width; x++)
        for (int y = 0; y < bmp1.Height; y++)
            
if (bmp1.GetPixel(x, y) != bmp2.GetPixel(x, y))  
                 iMismatch++;

   double percent = (double)iMismatch / (double)
          (bmp1.Height*bmp1.Width);
  
    return
percent * 100.0;
}

Approach TWO: UnSafe approach

public static double UnsafeCompare(ref Bitmap myBit, ref Bitmap myBit2)
{

System.Drawing.Imaging.BitmapData data = myBit.LockBits(new Rectangle(0, 0, myBit.Width, myBit.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, myBit.PixelFormat);

System.Drawing.Imaging.BitmapData data2 = myBit2.LockBits(new Rectangle(0, 0, myBit.Width, myBit.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, myBit.PixelFormat);

int iDifferent = 0;

int PixelSize = 4;

unsafe{

//REDUNDANT has happened later i correct :)
byte
* imgPtr = (