码迷,mamicode.com
首页 > 其他好文 > 详细

memory

时间:2019-01-27 21:39:29      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:vat   ali   mutex   mfile   nali   off   pst   getc   ota   

 

[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-c81\MMFeedHealthyDatacache\2016_07_10\Feedkeys\No_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

标签:vat   ali   mutex   mfile   nali   off   pst   getc   ota   

原文地址:https://www.cnblogs.com/chuncn/p/10327729.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!