memory
Posted chuncn
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了memory相关的知识,希望对你有一定的参考价值。
[DllImport("kernel32.dll")]
private static extern IntPtr GetCurrentProcess();
[DllImport("kernel32.dll")]
private static extern bool SetProcessWorkingSetSize(IntPtr proc, int min, int max);
private void ReleaseMemory()//FlushMemory()
{
GC.Collect();
GC.WaitForPendingFinalizers();
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
SetProcessWorkingSetSize(GetCurrentProcess(), -1, -1);
}
}
private static void SpiltFile(string srcFile, int portionSize)
{
string savedPath = @"\stcsrv-c81MMFeedHealthyDatacache2016_07_10FeedkeysNo_Process_test.txt";
FileInfo fi = new FileInfo(srcFile);
// total size in bytes
Int64 size = fi.Length;
object locker = new object();
object writeLock = new object();
List<MappedFile> mappedFiles = new List<MappedFile>();
Int64 fileToRead = size;//文件总的大小
portionSize = portionSize * 1024 * 1024; //每块大小
Int64 portion = (Int64)Math.Ceiling(size * 1.0 / portionSize); //分成多少块
Int64 fileOffset = 0;
MemoryMappedViewAccessor mmf_reader = null;
Stopwatch watch = Stopwatch.StartNew();
watch.Start();
Int64 fileSize = 0;
using (MemoryMappedFile mmf = MemoryMappedFile.CreateFromFile(srcFile, FileMode.Open, "xyz", size))
{
//using (var writeMap = MemoryMappedFile.CreateFromFile(savedPath, FileMode.Create, "test", size, MemoryMappedFileAccess.ReadWrite))
//{
//bool mutexCreated;
//Mutex mutex = new Mutex(true, "testmapmutex", out mutexCreated);//进程间同步
Parallel.For(0, portion, (i, ParallelLoopState) =>
{
//for (int i = 26; i < portion; i++)
//{
lock (locker)
{
fileSize = Math.Min(portionSize, fileToRead - portionSize * i);
if (fileSize > 0)
{
byte[] buffer;
using (mmf_reader = mmf.CreateViewAccessor(i * portionSize, fileSize, MemoryMappedFileAccess.Read))
{
buffer = new byte[fileSize];
mmf_reader.ReadArray(0, buffer, 0, (int)fileSize);
mappedFiles.Add(new MappedFile
{
Offset = i * portionSize, //fileOffset,
Buffer = buffer,
FileSize = fileSize
});
}
//fileToRead -= fileSize;
//lock (writeLock)
//{
//using (var writeMmf = MemoryMappedFile.OpenExisting("xyz"))
//{
// using (var writeAccessor = writeMmf.CreateViewStream(i * portionSize, fileSize))
// {
// var w = new BinaryWriter(new FileStream(savedPath, FileMode.Create, FileAccess.Write));
// //writeAccessor.WriteArray(i * portionSize, buffer, 0, buffer.Length);
// //writeAccessor.Write(buffer, 0, buffer.Length);
// w.Write(buffer);
// }
//}
//using (MemoryMappedViewAccessor writeView = writeMap.CreateViewAccessor())
//{
// writeView.WriteArray(i * portionSize, buffer, 0, (int)fileSize);
//}
}
//}
}
});
}
using (var writeMap = MemoryMappedFile.CreateFromFile(savedPath, FileMode.Create, "test", size, MemoryMappedFileAccess.ReadWrite))
{
using (MemoryMappedViewAccessor writeView = writeMap.CreateViewAccessor())
{
Parallel.For(0, mappedFiles.Count, i =>
{
try
{
Monitor.Enter(locker);
writeView.WriteArray(mappedFiles[i].Offset, mappedFiles[i].Buffer, 0, (int)mappedFiles[i].FileSize);
}
catch (Exception)
{
throw;
}
finally
{
Monitor.Exit(locker);
}
});
}
}
watch.Stop();
Console.WriteLine(watch.ElapsedMilliseconds);
#region MyRegion
}
以上是关于memory的主要内容,如果未能解决你的问题,请参考以下文章