Monthly Archives: September 2013

Beware of using RegexOptions.Compiled Regex in some situations

Using the RegexOptions.Compiled option in a Regex instance means that the regular expression is compiled into IL upon creation, which sounds good.

But beware, it can be an expensive operation which highlights itself if the code is called many times. If the Regex instance is reused a lot it makes sense to use this option, but even then beware that the time to create the compiled expression might not be worth it in a one-off scenario. In such cases do not use RegexOptions.Compiled.

Let’s look at some results of a simple test.

In the first example I’m simple creating the Regex in a method and calling the Split method on it, so these results obviously include the creation and use of the object. “Option” denotes whether the Regex was compiled or interpreted and the number column denotes is the number iterations calling we make creating a Regex and using it (time is shown in ms)

[table “” not found /]

Next we’ll create the Regex and reuse it

[table “” not found /]

Obviously what you see in the second table is that when the Regex Split is called a low number of times the performance gain of compiling the expression is minimal, but it gets progressively better the more times the regex is used.

So the lesson is to not use the RegexOptions.Compiled option unless you’re pretty sure the performance hit of creating the Regex isn’t an issue and it will be called a large number of times. All other times do not used RegexOptions.Compiled.

Note: performance tests were carried out on a 32-bit Windows XP machine

This setup requires Administrator privileges for configuring IIS Virtual Roots.

Came across this error “This setup requires Administrator privileges for configuring IIS Virtual Roots.” when attempting to install some software on a work development machine. Although I had admin rights and all permissions to install this app it failed with the above error.

To save on time getting support to sort it all out (and obviously should not be done if you don’t have the permission to do this) found that I could alter a registry setting the DisableUserInstalls value to 0 in HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\Installer.

Memory limits for a 32-bit .NET application

I’m currently working on an application which is deployed to Windows XP (yes some corporates are still on XP), so whilst I hope this post will be useless soon, I still currently need to worry about memory usage on a 32-bit machine.

Whilst Windows XP has a 4GB memory limit, not all of this is available to your application. An application/process on a 32-bit version of windows has 2GB of available memory to use (if using the /3G switch on Windows Server 2003 then you can get 3GB). You may start to see Out of Memory exceptions (obviously depending upon what you’re trying to allocate and garbage collection etc.) at around 1.2GB.

Whilst this application is currently running on a 32-bit version of Windows it may move to a 64-bit version soon. If it has to remain as a 32-bit application it will have double the amount of memory available. So a 32-bit application on a 64-bit version of Windows will have 4GB available and Out of memory exceptions may occur in the region of 2.8GB memory usage.

Here’s a little bit of code that allows us to get the memory usage for a process

public class MemoryInformation
{
[DllImport(“KERNEL32.DLL”)]
private static extern int OpenProcess(uint dwDesiredAccess,
int bInheritHandle, uint dwProcessId);
[DllImport(“KERNEL32.DLL”)]
private static extern int CloseHandle(int handle);

[StructLayout(LayoutKind.Sequential)]
private class PROCESS_MEMORY_COUNTERS
{
public int cb;
public int PageFaultCount;
public int PeakWorkingSetSize;
public int WorkingSetSize;
public int QuotaPeakPagedPoolUsage;
public int QuotaPagedPoolUsage;
public int QuotaPeakNonPagedPoolUsage;
public int QuotaNonPagedPoolUsage;
public int PagefileUsage;
public int PeakPagefileUsage;
}

[DllImport(“psapi.dll”)]
private static extern int GetProcessMemoryInfo(int hProcess,
[Out] PROCESS_MEMORY_COUNTERS counters, int size);

public static long GetMemoryUsageForProcess(long pid)
{
long mem = 0;
int pHandle = OpenProcess(0x0400 | 0x0010, 0, (uint)pid);
try
{
PROCESS_MEMORY_COUNTERS pmc = new PROCESS_MEMORY_COUNTERS();
if (GetProcessMemoryInfo(pHandle, pmc, 40) != 0)
mem = pmc.WorkingSetSize;
}
finally
{
CloseHandle(pHandle);
}
return mem;
}

public static string ToString(long numOfBytes)
{
double bytes = numOfBytes;
if (bytes < 1024) return bytes.ToString(); bytes /= 1024; if (bytes < 1024) { return bytes.ToString("#.# KB"); } else { bytes /= 1024; if (bytes < 1024) { return bytes.ToString("#.# MB"); } else { bytes /= 1024; return bytes.ToString("#.# GB"); } } } public static string GetFormattedMemoryUsageForProcess(long pid) { return ToString(GetMemoryUsageForProcess(pid)); } } [/code]

Oracle, where’s my data object

Had a strange issue with Oracle SQL Developer whereby it wasn’t showing a data object which I knew existed and realised I didn’t know the SQL to find it – sure SQL Developer does it all in the GUI but it’s nice to know how to do it in code, so here goes

select * from ALL_OBJECTS where OBJECT_NAME = 'SomeTableViewProcOrWhatever'