我想要做的是改变C#方法在调用时的执行方式,这样我就可以编写如下内容:
[Distributed]
public DTask<bool> Solve(int n, DEvent<bool> callback)
{
for (int m = 2; m < n - 1; m += 1)
if (m % n == 0)
return false;
return true;
}
Run Code Online (Sandbox Code Playgroud)
在运行时,我需要能够分析具有Distributed属性(我已经可以做)的方法,然后在函数体执行之前和函数返回之后插入代码.更重要的是,我需要能够在不修改调用Solve的代码的情况下或在函数的开头修改代码(在编译时;在运行时这样做是目标).
目前我尝试了这段代码(假设t是Solve存储的类型,m是Solve的MethodInfo):
private void WrapMethod(Type t, MethodInfo m)
{
// Generate ILasm for delegate.
byte[] il = typeof(Dpm).GetMethod("ReplacedSolve").GetMethodBody().GetILAsByteArray();
// Pin the bytes in the garbage collection.
GCHandle h = GCHandle.Alloc((object)il, GCHandleType.Pinned);
IntPtr addr = h.AddrOfPinnedObject();
int size = il.Length;
// Swap the method.
MethodRental.SwapMethodBody(t, m.MetadataToken, …Run Code Online (Sandbox Code Playgroud) 当我处理小文件(<= 32x32)时,我看到性能计数器"#Indinal GC"(在一个完美的应用程序中应保持为零)迅速增加WriteableBitmap.
虽然这不是一个小应用程序内部的重大瓶颈,但是当内存中存在数千个对象时,它变成了一个非常大的问题(应用程序冻结在99.75%"GC中的时间",在每个步骤几秒钟)(例如:EntityFramework上下文加载了许多实体和关系).
综合测试:
var objectCountPressure = (
from x in Enumerable.Range(65, 26)
let root = new DirectoryInfo((char)x + ":\\")
let subs =
from y in Enumerable.Range(0, 100 * IntPtr.Size)
let sub =new {DI = new DirectoryInfo(Path.Combine(root.FullName, "sub" + y)), Parent = root}
let files = from z in Enumerable.Range(0, 400) select new {FI = new FileInfo(Path.Combine(sub.DI.FullName, "file" + z)), Parent = sub}
select new {sub, files = files.ToList()}
select new {root, subs …Run Code Online (Sandbox Code Playgroud) 我已经阅读了有关此的多个线程,但仍然找不到任何有效的内容。
我正在编写基本上浏览图像数据库的程序。我有一个带有 DataTemplate 的 ListView:
<DataTemplate>
<Grid Width="Auto" Height="Auto" >
<Image VerticalAlignment="Center" Source="{Binding IsAsync=True,
Converter={StaticResource Converter},
ConverterParameter={x:Static viewModel:SearchViewModel.MiniaturesHeight}}"
Grid.RowSpan="2" Stretch="None" Margin="5"
Height="{Binding Source={StaticResource Locator}, Path=MiniaturesHeight}"
Width="{Binding Source={StaticResource Locator}, Path=MiniaturesHeight}"
RenderOptions.BitmapScalingMode="NearestNeighbor" />
<TextBlock Text="{Binding Name}" Margin="5" />
</Grid>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
在转换器中,我接收对象并根据其内容创建 URL。我的问题是我需要每页显示 100 个图像,例如整个数据库有 40k 个图像。我想允许用户单击所有页面而不会出现 StackOveflowException。不幸的是,每次我更改页面时,内存使用量都会增加并且不会下降,即使我等待很长时间也是如此。
程序本身使用大约 60MB 的 RAM,但在更改页面 5 次后,它的 RAM 达到 150MB,并且稳步上升。
这是我的第一个转换器:
public class ObjectToUrl : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
return DependencyProperty.UnsetValue;
var obj …Run Code Online (Sandbox Code Playgroud) 我构建控制台应用程序来进行这样的测试。
while (true)
{
var bmp = new Bitmap(1600, 1200);
//var buffer = new byte[2000 * 1000 * 4];
Thread.Sleep(10);
}
Run Code Online (Sandbox Code Playgroud)
运行后,内存在很短的时间内增加到2GB以上。
这是 案例的日志Bitmap。
[2017-04-27 12:15:03][00:00:01.0150128] PrivateBytes : 1583.0MB, AllHeapsBytes : 0.0MB, Thread Count : 23, CPU Usage : 12%
[2017-04-27 12:15:04][00:00:02.0019966] PrivateBytes : 2150.0MB, AllHeapsBytes : 0.0MB, Thread Count : 24, CPU Usage : 10%
[2017-04-27 12:15:05][00:00:03.0030021] PrivateBytes : 500.0MB, AllHeapsBytes : 4.1MB, Thread Count : 24, CPU Usage : 26%
[2017-04-27 12:15:06][00:00:04.0040047] PrivateBytes : 1043.0MB, AllHeapsBytes …Run Code Online (Sandbox Code Playgroud) c# ×3
wpf ×2
.net ×1
assemblies ×1
bitmap ×1
cil ×1
data-binding ×1
methods ×1
swap ×1
xaml ×1