找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1489|回复: 0

[分享] Reclaiming memory of erased objects

[复制链接]

已领礼包: 593个

财富等级: 财运亨通

发表于 2013-5-26 23:27:39 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
Reclaiming memory of erased objects                                                                                                        By Balaji Ramamoorthy
In AutoCAD, when an object is erased, it is marked as "erased" and the memory is not reclaimed until AutoCAD is closed. It is sometimes necessary to reclaim the memory  of such erased objects without having to close AutoCAD, especially when large number of entities are being created and erased.
Please note that reclaiming the memory shoud not be used unless you absolutely need to. Also, please refer to the ObjectARX documentation regarding its usage.
Here is a sample to that uses the "Database.ReclaimMemoryFromErasedObjects" to reclaim the memory. As explained in the ObjectARX documentation, in some scenarios, AutoCAD may not be able to reclaim memory of erased objects. To check if the reclaim was successful, a simple test is to try and open the reclaimed object. If the object was actually reclaimed, AutoCAD throws a "Autodesk.AutoCAD.Runtime.Exception" with the ErrorStatus set to "ErrorStatus.PermanentlyErased".
Here is a sample code. To try this, first run the "SetupHeavyModel" command that creates about 5000 spheres at random locations. These spheres can be erased and the memory reclaimed using the "CleanDb" command. Just to ensure that this code does not stall your system, I suggest changing the number of entities to a smaller number (say 200) and then increase it if you do not see a visually detectable amount of memory getting allocated in the system resource monitor.
  1. [CommandMethod("SetupHeavyModel")]
  2. static public void SetupHeavyModel()
  3. {
  4.     Document document =
  5.             Application.DocumentManager.MdiActiveDocument;
  6.     Editor editor = document.Editor;
  7.     Database db = document.Database;
  8.     using (Transaction tr
  9.         = document.TransactionManager.StartTransaction())
  10.     {
  11.         BlockTable bt = tr.GetObject(
  12.                                         db.BlockTableId,
  13.                                         OpenMode.ForRead
  14.                                     ) as BlockTable;
  15.         BlockTableRecord btr
  16.                         = tr.GetObject(
  17.                                         db.CurrentSpaceId,
  18.                                         OpenMode.ForWrite
  19.                                     ) as BlockTableRecord;

  20.         for (int cnt = 0; cnt < 5000; cnt++)
  21.         {
  22.             Solid3d sphere = new Solid3d();
  23.             sphere.SetDatabaseDefaults();
  24.             Random randomGen = new Random(cnt + 1);
  25.             double radius = randomGen.NextDouble() * 50.0;
  26.             sphere.CreateSphere(radius);
  27.             Matrix3d randomMover;
  28.             double xVec = randomGen.NextDouble() * 800;
  29.             double yVec = randomGen.NextDouble() * 700;
  30.             double zVec = randomGen.NextDouble() * 600;
  31.             randomMover
  32.                 = Matrix3d.Displacement(
  33.                                 new Vector3d(
  34.                                                 xVec,
  35.                                                 yVec,
  36.                                                 zVec
  37.                                             )
  38.                                        );
  39.             sphere.TransformBy(randomMover);
  40.             btr.AppendEntity(sphere);
  41.             tr.AddNewlyCreatedDBObject(sphere, true);
  42.         }
  43.         tr.Commit();
  44.     }
  45. }

  46. [CommandMethod("CleanDb")]
  47. static public void CleanupDbMethod()
  48. {
  49.    Document document =
  50.             Application.DocumentManager.MdiActiveDocument;
  51.     Editor editor = document.Editor;
  52.     Database db = document.Database;
  53.     using (ObjectIdCollection erased = new ObjectIdCollection())
  54.     {
  55.         using (Transaction tr
  56.                 = document.TransactionManager.StartTransaction())
  57.         {
  58.             BlockTable bt = tr.GetObject(
  59.                                             db.BlockTableId,
  60.                                             OpenMode.ForRead
  61.                                         ) as BlockTable;
  62.             BlockTableRecord btr = tr.GetObject
  63.                                         (
  64.                                             db.CurrentSpaceId,
  65.                                             OpenMode.ForRead
  66.                                         ) as BlockTableRecord;
  67.             foreach(ObjectId oid in btr)
  68.             {
  69.                 Entity ent = tr.GetObject(
  70.                                             oid,
  71.                                             OpenMode.ForWrite
  72.                                          ) as Entity;
  73.                 if (ent != null)
  74.                 {
  75.                     ent.Erase(true);
  76.                     erased.Add(oid);
  77.                 }
  78.             }
  79.             tr.Commit();
  80.         }
  81.         db.ReclaimMemoryFromErasedObjects(erased);
  82.         //// Test for reclaimed objects
  83.         //using (Transaction tr
  84.         //        = document.TransactionManager.StartTransaction())
  85.         //{
  86.         //    foreach (ObjectId oid in erased)
  87.         //    {
  88.         //        try
  89.         //        {
  90.         //            Entity ent = tr.GetObject
  91.         //                            (
  92.         //                                oid,
  93.         //                                OpenMode.ForRead
  94.         //                            ) as Entity;
  95.         //        }
  96.         //        catch (Autodesk.AutoCAD.Runtime.Exception ex)
  97.         //        {
  98.         //            if (ex.ErrorStatus !=
  99.         //                        ErrorStatus.PermanentlyErased)
  100.         //            {
  101.         //                editor.WriteMessage
  102.         //                    (
  103.         //                        "Not permanently erased !!"
  104.         //                    );
  105.         //            }
  106.         //        }
  107.         //    }
  108.         //    tr.Commit();
  109.         //}
  110.         erased.Clear();
  111.     }
  112. }


论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-9-21 10:46 , Processed in 0.278022 second(s), 31 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表