书接上文,

把代码改为多线程,

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。

下面也给出了解决方法。

http://stackoverflow.com/questions/9415955/c-sharp-working-with-entity-framework-in-a-multi-threaded-server

 

看完这个以后,我顿时明白了。

我的错误在于,为了所谓的性能考虑。

使用了

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的测试的更多相关文章

  1. MagicNotes:自我管理中的破窗效应

    MagicNotes,思绪随风飞扬,偶尔在这里停留. 在<程序员修炼之道——从小工到专家>这本书里,有这么一段描述: 在市区,有些建筑漂亮而整洁,而另一些却是破败不堪的“废弃船只”.为什么 ...

  2. 精力管理 | 迅速恢复精力的N个技巧,四个关键词以及自我管理的方法和工具列表

    精力管理 | 迅速恢复精力的N个技巧,所谓坚持,是坚定的“持有”,这个“持”字很值得琢磨——不是扛.不是顶,而是“持”这样一个半放松的状态.如果你没做好自己该做的事情,如果你自己没有成长起来,随着年龄 ...

  3. 【纯手工】整理豆瓣热点推荐列表-财经&自我管理

    [纯手工]整理豆瓣热点推荐列表-财经&自我管理  简七君 2013-10-27 09:40:06     豆瓣君的首页热点推荐实在难以捉摸,只有正好跳出推荐贴时才能按图索骥找列表.简七和小伙伴 ...

  4. [Azure DevOps] 管理测试计划、测试套件和测试用例

    我喜欢测试计划,它能让团队清楚测试进度,还能妥善分配测试人员,更重要的是它能保证测试质量和效率.Azure DevOps 里提供了 Test Plans 这个模块用于管理测试计划. 1. Azure ...

  5. 【ELK】Centos7 安装 ELK 7.6.2 和 UI 管理界面以及测试例子

    1. 初始化环境 1.0 初始化环境官网参考 https://www.elastic.co/guide/en/elasticsearch/reference/current/system-config ...

  6. 测试管理_出色测试管理者的思考[持续更新ing]

    如何合理安排并按质按量按时完成每一个测试任务,做好项目管理? 如何把控到每一个测试任务的质量? 如何快速构建和构建好测试环境? 如何获取或快速制作测试数据? 如何确保每一个测试人员的工作都饱满? 如何 ...

  7. 高并发秒杀系统--Service事务管理与继承测试

    [Spring IoC的类型及应用场景]  [Spring事务使用方式] [Spring事务的特性] [Spring事务回滚的理解] [Service声明式事务的配置] 1.配置事务管理器 2.配置基 ...

  8. kubernetes容器集群管理启动一个测试示例

    创建nginx 创建3个nginx副本 [root@master bin]# kubectl run nginx --image=nginx --replicas=3 kubectl run --ge ...

  9. Oracle管理监控之测试环境清理用户脚本

    --PL/SQL块删除用户 declare  cursor cur_duser is    select sid, serial# from v$session where username in ( ...

随机推荐

  1. 从SQL Server中清除msdb备份和恢复记录

    正如我在前面的技巧“您的数据库上次恢复是什么时候呢?”中提到的,SQL Server使msdb数据库内系统表中的备份和恢复记录保持激活状态.没有正常的维护,这些系统表将变得很大,从而导致对于msdb数 ...

  2. Python常用算法

    本节内容 算法定义 时间复杂度 空间复杂度 常用算法实例 1.算法定义 算法(Algorithm)是指解题方案的准确而完整的描述,是一系列解决问题的清晰指令,算法代表着用系统的方法描述解决问题的策略机 ...

  3. Unity3d平台信息设置

    [Unity3d平台信息设置] 通过"Edit" -> "Project Settings" -> "Player"菜单选项.打 ...

  4. 安装 Windows Service

    1.打开 VS 命令行窗口 2. installutil /u  service文件路径  (卸载原有服务) 3, installutil /i  service 文件路径 (安装服务)

  5. linux加入windows域之完美方案

    运行setup工具 认证配置 选择: “use winbind” “use kerberos” “use winbind authertication” 改为: 删除admin server 其余的改 ...

  6. 中国大学MOOC 玩转AutoCAD 熟悉AutoCAD的工作空间

  7. 使用clr 调用C#编写的dll中的方法的全解释

    使用clr 调用C#编写的dll中的方法的全解释1.数据库初始化:将下面这段代码直接在运行就可以初始化数据库了exec sp_configure 'show advanced options', '1 ...

  8. Sublime Text notes

    1. 设置在窗口右下方显示文件的编码,在user preferences里加上以下的配置 2.设置用新标签页打开新文件而不是用新窗口打开,将以下配置改为false(默认为true)

  9. Part5核心初始化_lesson3---关闭看门狗

    1.看门狗---作用 2.看门狗工作方式 3.原理图 时钟源来自于PCLK经过分频器,经过选择器,输出到作为看门狗定时器,WTDAT为一个预载值,当它计数为零的时候,还没有给WTDAT赋值,那么它会发 ...

  10. MSGPACK和PROTOBUF的故事(MSGPACK明显生产力不足)

    作者曾经在2014年测试出MSGPACK的关键字和中文字符有很大的冲突,所以后来放弃了,本文为很多年前写的一个对比,后来我们一直在使用HTTP协议和PROTOBUF. 看看MSGPACK的文档,自称效 ...