本文整理汇总了C#中System.Runtime.MemoryFailPoint类的典型用法代码示例。如果您正苦于以下问题:C# MemoryFailPoint类的具体用法?C# MemoryFailPoint怎么用?C# MemoryFailPoint使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
MemoryFailPoint类属于System.Runtime命名空间,在下文中一共展示了MemoryFailPoint类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。
示例1: Queue
//引入命名空间
using System;
using System.Runtime;
using System.IO;
using System.Threading;
using System.Collections.Generic;
using System.Collections;
class MemoryFailPointExample
{
// Allocate in chunks of 64 megabytes.
private const uint chunkSize = 64 << 20;
// Use more than the total user-available address space (on 32 bit machines)
// to drive towards getting an InsufficientMemoryException.
private const uint numWorkItems = 1 + ((1U << 31) / chunkSize);
static Queue workQueue = new Queue(50);
// This value can be computed separately and hard-coded into the application.
// The method is included to illustrate the technique.
private static int EstimateMemoryUsageInMB()
{
int memUsageInMB = 0;
long memBefore = GC.GetTotalMemory(true);
int numGen0Collections = GC.CollectionCount(0);
// Execute a test version of the method to estimate memory requirements.
// This test method only exists to determine the memory requirements.
ThreadMethod();
// Includes garbage generated by the worker function.
long memAfter = GC.GetTotalMemory(false);
// If a garbage collection occurs during the measuring, you might need a greater memory requirement.
Console.WriteLine("Did a GC occur while measuring? {0}", numGen0Collections == GC.CollectionCount(0));
// Set the field used as the parameter for the MemoryFailPoint constructor.
long memUsage = (memAfter - memBefore);
if (memUsage < 0)
{
Console.WriteLine("GC's occurred while measuring memory usage. Try measuring again.");
memUsage = 1 << 20;
}
// Round up to the nearest MB.
memUsageInMB = (int)(1 + (memUsage >> 20));
Console.WriteLine("Memory usage estimate: {0} bytes, rounded to {1} MB", memUsage, memUsageInMB);
return memUsageInMB;
}
static void Main()
{
Console.WriteLine("Attempts to allocate more than 2 GB of memory across worker threads.");
int memUsageInMB = EstimateMemoryUsageInMB();
// For a production application consider using the threadpool instead.
Thread[] threads = new Thread[numWorkItems];
// Create a work queue to be processed by multiple threads.
int n = 0;
for (n = 0; n < numWorkItems; n++)
workQueue.Enqueue(n);
// Continue to launch threads until the work queue is empty.
while (workQueue.Count > 0)
{
Console.WriteLine(" GC heap (live + garbage): {0} MB", GC.GetTotalMemory(false) >> 20);
MemoryFailPoint memFailPoint = null;
try
{
// Check for available memory.
memFailPoint = new MemoryFailPoint(memUsageInMB);
n = (int)workQueue.Dequeue();
threads[n] =
new Thread(new ParameterizedThreadStart(ThreadMethod));
WorkerState state = new WorkerState(n, memFailPoint);
threads[n].Start(state);
Thread.Sleep(10);
}
catch (InsufficientMemoryException e)
{
// MemoryFailPoint threw an exception, handle by sleeping for a while, then
// continue processing the queue.
Console.WriteLine("Expected InsufficientMemoryException thrown. Message: " + e.Message);
// We could optionally sleep until a running worker thread
// has finished, like this: threads[joinCount++].Join();
Thread.Sleep(1000);
}
}
Console.WriteLine("WorkQueue is empty - blocking to ensure all threads quit (each thread sleeps for 10 seconds)");
foreach (Thread t in threads)
t.Join();
Console.WriteLine("All worker threads are finished - exiting application.");
}
// Test version of the working code to determine memory requirements.
static void ThreadMethod()
{
byte[] bytes = new byte[chunkSize];
}
internal class WorkerState
{
internal int _threadNumber;
internal MemoryFailPoint _memFailPoint;
internal WorkerState(int threadNumber, MemoryFailPoint memoryFailPoint)
{
_threadNumber = threadNumber;
_memFailPoint = memoryFailPoint;
}
internal int ThreadNumber
{
get { return _threadNumber; }
}
internal MemoryFailPoint MemoryFailPoint
{
get { return _memFailPoint; }
}
}
// The method that does the work.
static void ThreadMethod(Object o)
{
WorkerState state = (WorkerState)o;
Console.WriteLine("Executing ThreadMethod, " +
"thread number {0}.", state.ThreadNumber);
byte[] bytes = null;
try
{
bytes = new byte[chunkSize];
// Allocated all the memory needed for this workitem.
// Now dispose of the MemoryFailPoint, then process the workitem.
state.MemoryFailPoint.Dispose();
}
catch (OutOfMemoryException oom)
{
Console.Beep();
Console.WriteLine("Unexpected OutOfMemory exception thrown: " + oom);
}
// Do work here, possibly taking a lock if this app needs
// synchronization between worker threads and/or the main thread.
// Keep the thread alive for awhile to simulate a running thread.
Thread.Sleep(10000);
// A real thread would use the byte[], but to be an illustrative sample,
// explicitly keep the byte[] alive to help exhaust the memory.
GC.KeepAlive(bytes);
Console.WriteLine("Thread {0} is finished.", state.ThreadNumber);
}
}
开发者ID:.NET开发者,项目名称:System.Runtime,代码行数:150,代码来源:MemoryFailPoint
示例2: new MemoryFailPoint
//引入命名空间
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
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");
}
}
}
开发者ID:C#程序员,项目名称:System.Runtime,代码行数:24,代码来源:MemoryFailPoint
注:本文中的System.Runtime.MemoryFailPoint类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论