Message 1 of 14
Not applicable
12-14-2017
02:13 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
how to free the memory with ReclaimMemoryFromErasedObjects ? I try to load file then delete all objects , But I find the memoey increase more and more? what's wrong?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.Gis.Map.Platform.Utils;
namespace ReclaimMemory
{
public class Class1
{
[CommandMethod("hello")]
public void hello()
{
Util.PrintLn("com to hello");
}
[CommandMethod("MyMemory")]
public void myMemory()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database destDb = Application.DocumentManager.MdiActiveDocument.Database;
for (int i = 0; i < 50; i++)
{
string filename = "C:\\110344.dwg";
Util.PrintLn("file"+i);
Database sourceDb = new Database(false, true);
sourceDb.ReadDwgFile(filename, System.IO.FileShare.Read, false, null);
var SourceObjectIds = new ObjectIdCollection();
Autodesk.AutoCAD.DatabaseServices.TransactionManager SourceTm = sourceDb.TransactionManager;
using (Transaction trans = destDb.TransactionManager.StartTransaction())
{
try
{
var blockTable = (BlockTable)trans.GetObject(destDb.BlockTableId, OpenMode.ForRead);
using (Transaction extTrans = sourceDb.TransactionManager.StartTransaction())
{
dynamic extBlockTable = (BlockTable)extTrans.GetObject(sourceDb.BlockTableId, OpenMode.ForRead);
dynamic extModelSpace = (BlockTableRecord)extTrans.GetObject(extBlockTable[BlockTableRecord.ModelSpace], OpenMode.ForRead);
foreach (ObjectId id in extModelSpace)
{
SourceObjectIds.Add(id);
}
}
sourceDb.CloseInput(true);
dynamic idmapping = new IdMapping();
// destDb.WblockCloneObjects(SourceObjectIds, destDb.BlockTableId, idmapping, DuplicateRecordCloning.Replace, false);
destDb.WblockCloneObjects(SourceObjectIds, blockTable[BlockTableRecord.ModelSpace], idmapping, DuplicateRecordCloning.Ignore, false);
trans.Commit();
}
catch (System.Exception ex)
{
Util.PrintLn(ex.Message);
}
}
string saveFile ="C:\\"+ i + ".dwg";
destDb.SaveAs(saveFile, true, DwgVersion.AC1800, null);
//**************delete objects and free memory
using (ObjectIdCollection erased = new ObjectIdCollection())
{
using (Transaction trans = destDb.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(destDb.BlockTableId, OpenMode.ForWrite);
foreach (ObjectId btrid in bt)
{
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(btrid, OpenMode.ForWrite);
foreach (ObjectId eid in btr)
{
Entity ent = trans.GetObject(eid, OpenMode.ForWrite) as Entity;
if (ent != null)
{
ent.Erase(true);
erased.Add(eid);
}
}
}
trans.Commit();
}
destDb.ReclaimMemoryFromErasedObjects(erased);
erased.Clear();
}
}
}
}
}
Solved! Go to Solution.
