Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to prevent saving the same file multiple times in the memory
    text
    copied!<p>i want to read files (1 gb) per byte by using MemoryMappedFile from couple of threads (and processes ! ), my example code:</p> <pre><code>static unsafe void T() { MemoryMappedFile mmf = null; var mmfName = "q1"; var fname = @"%file_name%"; lock (Lock) { try { mmf = MemoryMappedFile.OpenExisting(mmfName, MemoryMappedFileRights.Read, HandleInheritability.Inheritable); Console.WriteLine("ok - open"); } catch { FileStream fs = null; try { fs = new FileStream(fname, System.IO.FileMode.Open, FileAccess.Read, FileShare.Read, 4 * 1024, true); mmf = MemoryMappedFile.CreateFromFile(fs, mmfName, 0, MemoryMappedFileAccess.Read, null, HandleInheritability.Inheritable, false); Console.WriteLine("ok - load"); } catch { Console.WriteLine("fail"); } } } var size = new System.IO.FileInfo(fname).Length; byte* ptr = (byte*)0; var vh = mmf.CreateViewAccessor(0, 0, MemoryMappedFileAccess.Read).SafeMemoryMappedViewHandle; vh.AcquirePointer(ref ptr); var rnd = new Random(); for (int i = 0; i &lt; size; i++) { var t = *(ptr + (int)rnd.Next(0, (int)size)); } } </code></pre> <p>and code to run the test:</p> <pre><code> static unsafe void Main(string[] args) { Parallel.Invoke(T, T, T); Console.WriteLine("done"); Console.ReadLine(); } </code></pre> <p>app output:</p> <pre><code>ok - load ok - open ok - open </code></pre> <p>The problem that my app takes 3 gb in memory (working set). So it copies file 3 times in memory. I want only one copy in memory (1 gb) for "ok - load" thread, and no copies for reading ("ok - open") threads.</p> <p>Any ideas? (c#, 4.5, vs12, win7)</p> <p>Thanks for help </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload