// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Diagnostics; using System.Runtime.InteropServices; namespace E { public class Program { [DllImport(@"C:\Job.dll")] private static extern IntPtr setup(int mem); [DllImport(@"C:\Job.dll")] private static extern void teardown(IntPtr job); private const int N = 25000000; private static void Test(byte[] ba) { for(int i = 0; i < ba.Length; i++) ba[i] = 123; } private static void Test1() { byte[] b1 = new byte[N]; Test(b1); b1 = null; byte[] b2 = new byte[N]; Test(b2); } private static void Test2() { byte[] b1 = new byte[N]; Test(b1); byte[] b2 = new byte[N]; Test(b2); } public static void Main(string[] args) { IntPtr job = IntPtr.Zero; try { job = setup((int)(1.5*N)); Test1(); Console.WriteLine("Test1 OK"); Test2(); Console.WriteLine("Test2 OK"); } catch(OutOfMemoryException) { Console.WriteLine("Oops"); } finally { if(job != IntPtr.Zero) teardown(job); } } } }