对于自我管理 ObjectContextManager的测试
书接上文,
把代码改为多线程,
public class Threads
{
public static void allStart()
{
for (int i = 0; i < 10; i++)
{
t1();
}
}
public static void t1()
{
var t = new Thread(() =>
{
Class1.Test();
});
t.IsBackground = true;
t.Start();
}
}
内在状况:

看了一下,CPU相当的同,但是内存已经没问题了。
现在从读的情况来看没什么问题。
把代码再改改。
public class Class1
{
private static Domain.Entities.Models.passportContext context;
static Class1()
{
context =
new Domain.Entities.Models.passportContext("passportContext");
context.Configuration.ProxyCreationEnabled = false;
context.Configuration.LazyLoadingEnabled = false;
var om = ((IObjectContextAdapter)context).ObjectContext.ObjectStateManager;
om.ObjectStateManagerChanged += om_ObjectStateManagerChanged;
}
public static void Test()
{
var start = int.MaxValue;
while (true)
{
var list = context.UserSources.OrderByDescending(x => x.UserId).Where(x => x.UserId < start).Take(100).ToList();
ObjectContext oc = ((IObjectContextAdapter)context).ObjectContext;
var m = oc.ObjectStateManager;
var a = list.First();
var count = GetUnchangedCount(context);
ClearUnchangedCache(context);
ClearKeylessEntityCache(context);
count = GetUnchangedCount(context);
Console.WriteLine(string.Format("[{0}]", Thread.CurrentThread.ManagedThreadId) + a.UserId);
//context.SaveChanges();
//ClearCache(context);
start = a.UserId;
}
}
然后这样就,呵呵了。

是的,对于多线程下,不能使用共享的Context。
下面也给出了解决方法。
看完这个以后,我顿时明白了。
我的错误在于,为了所谓的性能考虑。
使用了
public abstract class StatBase<TSource, TStat>
where TSource : class
where TStat : class
{
protected readonly GenericRepository<TSource> Sourcelog;
protected readonly GenericRepository<TStat> Stat;
protected readonly SingleFileLine Sfl;
private int sleep = 45;
public event EventHandler<InfoEventArgs> StateInfo;
protected virtual void OnStateInfo(InfoEventArgs e)
{
EventHandler<InfoEventArgs> handler = this.StateInfo;
if (handler != null) handler(this, e);
}
protected virtual void OnState(InfoEventArgs e)
{
EventHandler<InfoEventArgs> handler = this.StateInfo;
if (handler != null) handler(this, e);
}
protected virtual void OnState(string info)
{
EventHandler<InfoEventArgs> handler = this.StateInfo;
if (handler != null) handler(this, new InfoEventArgs
{
Info = info,
Time = System.DateTime.Now
});
}
protected StatBase()
{
this.Sourcelog = new GenericRepository<TSource>(AppIniter.DefaultConnection);
this.Stat = new GenericRepository<TStat>(AppIniter.DefaultConnection);
this.Sfl = new SingleFileLine(typeof(TStat).Name);
}
在构造中对Responstory进行实例化,然后再多线程中使用,所以就出现了上个文章中的问题。内存不断的增长。
那这样,改下程序,把实例化的过程放到线程中,每次使用实例化一个对象,然后超出作用域后就自动释放。
这是一个好的想法,但不帅了,不过,可以试试的。
代码就变成 了这样。
public abstract class StatBase<TSource, TStat>
where TSource : class
where TStat : class
{
protected GenericRepository<TSource> Sourcelog
{
get
{
return new GenericRepository<TSource>(AppIniter.DefaultConnection);
}
}
protected GenericRepository<TStat> Stat
{
get
{
return new GenericRepository<TStat>(AppIniter.DefaultConnection);
}
}
但是处理速度上明显就下去了。
*》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
其实整体上没有解决,没有按上面的路走下去,直接使用的就是 using(context)
对于自我管理 ObjectContextManager的测试的更多相关文章
- MagicNotes:自我管理中的破窗效应
MagicNotes,思绪随风飞扬,偶尔在这里停留. 在<程序员修炼之道——从小工到专家>这本书里,有这么一段描述: 在市区,有些建筑漂亮而整洁,而另一些却是破败不堪的“废弃船只”.为什么 ...
- 精力管理 | 迅速恢复精力的N个技巧,四个关键词以及自我管理的方法和工具列表
精力管理 | 迅速恢复精力的N个技巧,所谓坚持,是坚定的“持有”,这个“持”字很值得琢磨——不是扛.不是顶,而是“持”这样一个半放松的状态.如果你没做好自己该做的事情,如果你自己没有成长起来,随着年龄 ...
- 【纯手工】整理豆瓣热点推荐列表-财经&自我管理
[纯手工]整理豆瓣热点推荐列表-财经&自我管理 简七君 2013-10-27 09:40:06 豆瓣君的首页热点推荐实在难以捉摸,只有正好跳出推荐贴时才能按图索骥找列表.简七和小伙伴 ...
- [Azure DevOps] 管理测试计划、测试套件和测试用例
我喜欢测试计划,它能让团队清楚测试进度,还能妥善分配测试人员,更重要的是它能保证测试质量和效率.Azure DevOps 里提供了 Test Plans 这个模块用于管理测试计划. 1. Azure ...
- 【ELK】Centos7 安装 ELK 7.6.2 和 UI 管理界面以及测试例子
1. 初始化环境 1.0 初始化环境官网参考 https://www.elastic.co/guide/en/elasticsearch/reference/current/system-config ...
- 测试管理_出色测试管理者的思考[持续更新ing]
如何合理安排并按质按量按时完成每一个测试任务,做好项目管理? 如何把控到每一个测试任务的质量? 如何快速构建和构建好测试环境? 如何获取或快速制作测试数据? 如何确保每一个测试人员的工作都饱满? 如何 ...
- 高并发秒杀系统--Service事务管理与继承测试
[Spring IoC的类型及应用场景] [Spring事务使用方式] [Spring事务的特性] [Spring事务回滚的理解] [Service声明式事务的配置] 1.配置事务管理器 2.配置基 ...
- kubernetes容器集群管理启动一个测试示例
创建nginx 创建3个nginx副本 [root@master bin]# kubectl run nginx --image=nginx --replicas=3 kubectl run --ge ...
- Oracle管理监控之测试环境清理用户脚本
--PL/SQL块删除用户 declare cursor cur_duser is select sid, serial# from v$session where username in ( ...
随机推荐
- axis调用webservice客户端开发
第一步:wsdl2Java.bat文件编写 Axis_Lib表示依赖的jar包路径 Output_Path表示生成的class路径 Package包名 还需要手动更改 -p %Package%表示we ...
- 成都国嵌-嵌入式linux必修实验手册…
emouse收集整理,转载请注明: emouse的技术专栏 博客园:http://www.cnblogs.com/emouse/ CSDN:http://blog.csdn.net/haozi_198 ...
- WordPress,discuz 根据不同的入口url更换logo
Discuz!中调用cookie的思路出来了: 设置cookie:dsetcookie('cookie名', 'cookie值', '有效时间'); 读取cookie有两种方法,第一种使用get ...
- HttpRuntime自定义定时更新缓存
缓存更新类如下: /// <summary> /// 缓存更新类 /// </summary> /// <typeparam name="T"> ...
- codeforce465DIV2——D. Fafa and Ancient Alphabet
概率的计算答案给出的这张图很清楚了,然后因为要求取模,a/b%M=a*b^-1%M=a*inv(b,M)%M; #include <cstdio> #include <cstring ...
- 643. Maximum Average Subarray I 最大子数组的平均值
[抄题]: Given an array consisting of n integers, find the contiguous subarray of given length k that h ...
- Solidity oraclize query apikey加密
solidity 程序中如果用到oraclize query,api调用需要apikey,则最好加密apikey,否则公开solidity代码时会连同apikey一起公开. 加密方法: https:/ ...
- dataframe 用法总结
http://pda.readthedocs.io/en/latest/chp5.html data = [] 列表初始化 data = (,) data = {} 字典初始化 data = pd.D ...
- Cookie存中文乱码的问题
有个奇怪的问题:登录页面中使用Cookie存值,Cookie中要存中文汉字.代码在本地调试,一切OK,汉字也能顺利存到Cookie和从Cookie中读出,但是放到服务器上不管用了,好好的汉字成了乱码, ...
- Django框架 之 admin管理工具(组件使用)
Django框架 之 admin管理工具(组件使用) 浏览目录 激活管理工具 使用管理工具 admin的定制 admin组件使用 Django 提供了基于 web 的管理工具. Django 自动管理 ...