Csharp/CSharp Tutorial/Windows/Memory

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Allocate and free memory

<source lang="csharp">using System; using System.Runtime.InteropServices; class MainClass {

 static void Main(string[] args)
 {
   UsePointers();
 }
 static unsafe public void UsePointers()
 {
   char * pMyArray = (char*)Marshal.AllocCoTaskMem(6);
   while (*pMyArray != "\0")
   {
     Console.WriteLine(*pMyArray);
     pMyArray++;
   }
   Marshal.FreeCoTaskMem((IntPtr)pMyArray);
 }

}</source>

Allocate memory with System.Runtime.MemoryFailPoint

<source lang="csharp">using System; using System.Collections.Generic; using System.Diagnostics; using System.Runtime.rupilerServices; public class MainClass {

   public static void Main()
   {
       using (System.Runtime.MemoryFailPoint gate = new System.Runtime.MemoryFailPoint(100))
       {
           // Some operation that actually uses the 100MB of memory�
           Console.WriteLine("Success for 100MB fail-point");
       }
       using (System.Runtime.MemoryFailPoint gate = new System.Runtime.MemoryFailPoint(1024*100))
       {
           // Some operation that actually uses the 100GB of memory�
           Console.WriteLine("Success for 100GB fail-point");
       }
   }

}</source>

Create Heap and destroy Heap

<source lang="csharp">using System; using System.Runtime.InteropServices; public sealed class MainClass {

   [DllImport("kernel32.dll")]
   static extern IntPtr HeapCreate(uint flOptions, UIntPtr dwInitialSize,UIntPtr dwMaximumSize);
   [DllImport("kernel32.dll")]
   static extern bool HeapDestroy(IntPtr hHeap);
   public static void Main() {
       IntPtr theHeap = HeapCreate( 0, (UIntPtr) 4096, UIntPtr.Zero );
       HeapDestroy( theHeap );
       theHeap = IntPtr.Zero;
   }

}</source>

Demonstrate stackalloc

<source lang="csharp">using System;

class MainClass {

 unsafe public static void Main() { 
   int* ptrs = stackalloc int[3]; 

   ptrs[0] = 1; 
   ptrs[1] = 2; 
   ptrs[2] = 3; 

   for(int i=0; i < 3; i++) 
     Console.WriteLine(ptrs[i]); 
 } 

}</source>

1
2
3

Free Memory

<source lang="csharp">using System; using System.IO; using System.Reflection; using System.Runtime.ConstrainedExecution; using System.Runtime.InteropServices; using Microsoft.Win32.SafeHandles; public class MainClass {

   public static void Main()
   {
       IntPtr ptr = Marshal.AllocHGlobal(1024);
       GC.AddMemoryPressure(1024);
       if (ptr != IntPtr.Zero) {
           Marshal.FreeHGlobal(ptr);
           ptr = IntPtr.Zero;
           GC.RemoveMemoryPressure(1024);
       }
   }

}</source>

Free memory: Marshal.FreeHGlobal

<source lang="csharp">using System; using System.IO; using System.Reflection; using System.Runtime.ConstrainedExecution; using System.Runtime.InteropServices; using Microsoft.Win32.SafeHandles; public class MainClass {

   public static void Main()
   {
       IntPtr ptr = Marshal.AllocHGlobal(1024);
       if (ptr != IntPtr.Zero){
               Marshal.FreeHGlobal(ptr);
               ptr = IntPtr.Zero;
       }
   }

}</source>