<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: database  free the memory in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/database-free-the-memory/m-p/7681461#M28612</link>
    <description>&lt;P&gt;i am sorry , &amp;nbsp;My&amp;nbsp; English &amp;nbsp;is poor&amp;nbsp;&lt;/P&gt;&lt;P&gt;the &amp;nbsp;actual &amp;nbsp; code &amp;nbsp; &amp;nbsp;like &amp;nbsp;this&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;BR /&gt;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 &amp;lt; 50; i++)
            {
                //string filename = "C:\\110344.dwg";
                string filename = "C:\\110344"+i+".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();
                }


            }

        }
    }
}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;i &amp;nbsp;change &amp;nbsp;the code &amp;nbsp;"string &amp;nbsp; filename="C:\\110334.dwg"" &amp;nbsp; to &amp;nbsp; "string &amp;nbsp;filename="c:\\110334"+i+".dwg""&lt;/P&gt;&lt;P&gt;every time &amp;nbsp;,I &amp;nbsp;load &amp;nbsp;the &amp;nbsp;different &amp;nbsp;file&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 11 Jan 2018 08:41:59 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2018-01-11T08:41:59Z</dc:date>
    <item>
      <title>database  free the memory</title>
      <link>https://forums.autodesk.com/t5/net-forum/database-free-the-memory/m-p/7623187#M28604</link>
      <description>&lt;P&gt;how to &amp;nbsp;free the memory &amp;nbsp;with ReclaimMemoryFromErasedObjects&amp;nbsp;? &amp;nbsp; I try to &amp;nbsp;load file &amp;nbsp;then &amp;nbsp; &amp;nbsp;delete all objects &amp;nbsp;, &amp;nbsp;But &amp;nbsp;I find &amp;nbsp;the memoey &amp;nbsp;increase more and more? &amp;nbsp;what's wrong?&lt;/P&gt;&lt;PRE&gt;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 &amp;lt; 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();
                }


            }

        }
    }
}&lt;/PRE&gt;</description>
      <pubDate>Thu, 14 Dec 2017 10:13:33 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/database-free-the-memory/m-p/7623187#M28604</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2017-12-14T10:13:33Z</dc:date>
    </item>
    <item>
      <title>Re: database  free the memory</title>
      <link>https://forums.autodesk.com/t5/net-forum/database-free-the-memory/m-p/7624297#M28605</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;@Anonymous wrote:&lt;BR /&gt;&lt;P&gt;how to &amp;nbsp;free the memory &amp;nbsp;with ReclaimMemoryFromErasedObjects&amp;nbsp;? &amp;nbsp; I try to &amp;nbsp;load file &amp;nbsp;then &amp;nbsp; &amp;nbsp;delete all objects &amp;nbsp;, &amp;nbsp;But &amp;nbsp;I find &amp;nbsp;the memoey &amp;nbsp;increase more and more? &amp;nbsp;what's wrong?&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;It's hard to tell what's wrong without testing your code, but I would suggest reading the &lt;A href="http://help.autodesk.com/view/OARX/2018/ENU/?guid=OREF-AcDbDatabase__reclaimMemoryFromErasedObjects_AcDbObjectIdArray_" target="_blank"&gt;native API docs for reclaimMemoryFromErasedObjects()&lt;/A&gt;, because it goes into much greater detail than the managed API docs.&lt;/P&gt;</description>
      <pubDate>Thu, 14 Dec 2017 16:17:04 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/database-free-the-memory/m-p/7624297#M28605</guid>
      <dc:creator>ActivistInvestor</dc:creator>
      <dc:date>2017-12-14T16:17:04Z</dc:date>
    </item>
    <item>
      <title>Re: database  free the memory</title>
      <link>https://forums.autodesk.com/t5/net-forum/database-free-the-memory/m-p/7680997#M28606</link>
      <description>&lt;P&gt;i find &amp;nbsp;the memory &amp;nbsp;begin to be &amp;nbsp;free , when &amp;nbsp;all &amp;nbsp;the &amp;nbsp;50 files work finish...&lt;/P&gt;&lt;P&gt;i &amp;nbsp;want &amp;nbsp;to &amp;nbsp;that &amp;nbsp;when &amp;nbsp;one &amp;nbsp;file &amp;nbsp;finish work ,the memory &amp;nbsp;begin &amp;nbsp;to free &amp;nbsp;immediately，so &amp;nbsp;the &amp;nbsp;max &amp;nbsp;memory &amp;nbsp;overhead is &amp;nbsp;not so large&lt;/P&gt;&lt;P&gt;so &amp;nbsp;the &amp;nbsp; "ReclaimMemoryFromErasedObjects" &amp;nbsp;is not &amp;nbsp;usefull &amp;nbsp;for &amp;nbsp;me ?&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jan 2018 03:39:55 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/database-free-the-memory/m-p/7680997#M28606</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2018-01-11T03:39:55Z</dc:date>
    </item>
    <item>
      <title>Re: database  free the memory</title>
      <link>https://forums.autodesk.com/t5/net-forum/database-free-the-memory/m-p/7681188#M28607</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Just a shoot in the dark, not tested with a big amount of files.&lt;/P&gt;
&lt;P&gt;What if you call Dispose() on the newly created Database, or, better wrap int in a using statement:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;using(Database sourceDb = new Database(false, true))&lt;BR /&gt;{&lt;BR /&gt;    // use sourceDb&lt;BR /&gt;} // sourceDb is disposed here and can be garbage collected&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jan 2018 06:35:43 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/database-free-the-memory/m-p/7681188#M28607</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2018-01-11T06:35:43Z</dc:date>
    </item>
    <item>
      <title>Re: database  free the memory</title>
      <link>https://forums.autodesk.com/t5/net-forum/database-free-the-memory/m-p/7681223#M28608</link>
      <description>&lt;P&gt;i &amp;nbsp;change &amp;nbsp;code like this&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;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 &amp;lt; 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();&lt;BR /&gt;                        //**************add   dispose()&lt;BR /&gt;                        sourceDb.Dispose();&lt;BR /&gt;                        SourceObjectIds.Dispose();
                    }
                    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);&lt;BR /&gt;&lt;BR /&gt;                     //********* add dispose()&lt;BR /&gt;                   destDb.Dispose();&lt;BR /&gt;                    
                    erased.Clear();&lt;BR /&gt;                   
                }


            }

        }
    }
}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp; dispose &amp;nbsp;function &amp;nbsp;seem &amp;nbsp;to be unuseful &amp;nbsp;.&lt;/P&gt;&lt;P&gt;&amp;nbsp; the file &amp;nbsp;of "C:\\110334.dwg" &amp;nbsp;is &amp;nbsp;1.9MB , in the &amp;nbsp;working &amp;nbsp;processing , The most &amp;nbsp;memory overhead &amp;nbsp;of acad.exe &amp;nbsp; is more than &amp;nbsp;3G !&lt;/P&gt;&lt;P&gt;&amp;nbsp;when all &amp;nbsp;the work &amp;nbsp;finish , &amp;nbsp;the acad.exe &amp;nbsp;memory &amp;nbsp;overhead &amp;nbsp;reduce to about &amp;nbsp;900M&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jan 2018 07:03:29 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/database-free-the-memory/m-p/7681223#M28608</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2018-01-11T07:03:29Z</dc:date>
    </item>
    <item>
      <title>Re: database  free the memory</title>
      <link>https://forums.autodesk.com/t5/net-forum/database-free-the-memory/m-p/7681364#M28609</link>
      <description>&lt;P&gt;Looking more attentively to your code, it seems to me it needs some algorithmic and coding optimisation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You do not need to create 50 side databases to get get everytime the same ObjectIdCollection.&lt;/P&gt;
&lt;P&gt;You should create 50 side databases to create the new dwg files.&lt;/P&gt;
&lt;P&gt;You should not use the dynamic type for strongly typed objects (use the type or var keyword instead).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try something like this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;        [CommandMethod("MyMemory")]
        public void myMemory()
        {
            // Get the source ObjectIds from sourceDb only once
            var sourceObjectIds = new ObjectIdCollection();
            string filename = "C:\\110344.dwg";
            using (Database sourceDb = new Database(false, true))
            {
                sourceDb.ReadDwgFile(filename, FileOpenMode.OpenForReadAndAllShare, false, null);
                using (Transaction extTrans = sourceDb.TransactionManager.StartOpenCloseTransaction())
                {
                    var extBlockTable = (BlockTable)extTrans.GetObject(sourceDb.BlockTableId, OpenMode.ForRead);
                    var extModelSpace = (BlockTableRecord)extTrans.GetObject(extBlockTable[BlockTableRecord.ModelSpace], OpenMode.ForRead);
                    foreach (ObjectId id in extModelSpace)
                    {
                        sourceObjectIds.Add(id);
                    }
                }
                // Loop to create the 50 new files
                for (int i = 0; i &amp;lt; 50; i++)
                {
                    Util.PrintLn("file" + i);
                    string saveFile = "C:\\" + i + ".dwg";
                    using (var destDb = new Database(true, true))
                    {
                        var mapping = new IdMapping();
                        sourceDb.WblockCloneObjects(
                            sourceObjectIds, 
                            SymbolUtilityServices.GetBlockModelSpaceId(destDb), 
                            mapping, 
                            DuplicateRecordCloning.Ignore, 
                            false);
                        destDb.SaveAs(saveFile, true, DwgVersion.AC1800, null);
                    }
                }
            }
        }&lt;/PRE&gt;</description>
      <pubDate>Thu, 11 Jan 2018 07:58:13 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/database-free-the-memory/m-p/7681364#M28609</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2018-01-11T07:58:13Z</dc:date>
    </item>
    <item>
      <title>Re: database  free the memory</title>
      <link>https://forums.autodesk.com/t5/net-forum/database-free-the-memory/m-p/7681399#M28610</link>
      <description>&lt;P&gt;thank &amp;nbsp;you for reply!&lt;/P&gt;&lt;P&gt;oh~~~~ as &amp;nbsp;your code ,maybe &amp;nbsp;much better.&lt;/P&gt;&lt;P&gt;Actually， very time &amp;nbsp;the &amp;nbsp;file &amp;nbsp;loaded &amp;nbsp; is different, &amp;nbsp;it &amp;nbsp;just &amp;nbsp;a &amp;nbsp;test , so &amp;nbsp;i &amp;nbsp;get &amp;nbsp;the &amp;nbsp;same &amp;nbsp;file &amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Actually,&lt;/SPAN&gt;I have more &amp;nbsp;than 50 different &amp;nbsp;files ,then &amp;nbsp;operate the &amp;nbsp;file &amp;nbsp;,then &amp;nbsp;save &amp;nbsp;and &amp;nbsp;close, one by one&amp;nbsp;&lt;/P&gt;&lt;P&gt;can you understand &amp;nbsp;what i mean?&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jan 2018 08:17:06 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/database-free-the-memory/m-p/7681399#M28610</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2018-01-11T08:17:06Z</dc:date>
    </item>
    <item>
      <title>Re: database  free the memory</title>
      <link>https://forums.autodesk.com/t5/net-forum/database-free-the-memory/m-p/7681433#M28611</link>
      <description>&lt;P&gt;Sorry I cannot understand what you're trying to do.&lt;/P&gt;
&lt;P&gt;The code I purposed is an optimisation of the one you posted (creating 50 new files with the content of the sourceDB model space). If this does not reflect what you're trying to achieve, why do you use it as testing code ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So please, just try to clearly explain what you want you to so that we're not shooting in the dark...&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jan 2018 08:30:03 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/database-free-the-memory/m-p/7681433#M28611</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2018-01-11T08:30:03Z</dc:date>
    </item>
    <item>
      <title>Re: database  free the memory</title>
      <link>https://forums.autodesk.com/t5/net-forum/database-free-the-memory/m-p/7681461#M28612</link>
      <description>&lt;P&gt;i am sorry , &amp;nbsp;My&amp;nbsp; English &amp;nbsp;is poor&amp;nbsp;&lt;/P&gt;&lt;P&gt;the &amp;nbsp;actual &amp;nbsp; code &amp;nbsp; &amp;nbsp;like &amp;nbsp;this&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;BR /&gt;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 &amp;lt; 50; i++)
            {
                //string filename = "C:\\110344.dwg";
                string filename = "C:\\110344"+i+".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();
                }


            }

        }
    }
}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;i &amp;nbsp;change &amp;nbsp;the code &amp;nbsp;"string &amp;nbsp; filename="C:\\110334.dwg"" &amp;nbsp; to &amp;nbsp; "string &amp;nbsp;filename="c:\\110334"+i+".dwg""&lt;/P&gt;&lt;P&gt;every time &amp;nbsp;,I &amp;nbsp;load &amp;nbsp;the &amp;nbsp;different &amp;nbsp;file&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jan 2018 08:41:59 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/database-free-the-memory/m-p/7681461#M28612</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2018-01-11T08:41:59Z</dc:date>
    </item>
    <item>
      <title>Re: database  free the memory</title>
      <link>https://forums.autodesk.com/t5/net-forum/database-free-the-memory/m-p/7681653#M28613</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;p&lt;SPAN&gt;lease commit all your transaction even if it is not really necessary.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Look here:&amp;nbsp;&lt;A href="http://spiderinnet1.typepad.com/blog/2012/01/autocad-net-transaction-and-performance.html" target="_blank"&gt;http://spiderinnet1.typepad.com/blog/2012/01/autocad-net-transaction-and-performance.html&lt;/A&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;When you commit it is much faster and saver.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Regards Jürgen&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jan 2018 10:04:36 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/database-free-the-memory/m-p/7681653#M28613</guid>
      <dc:creator>Juergen_Becker</dc:creator>
      <dc:date>2018-01-11T10:04:36Z</dc:date>
    </item>
    <item>
      <title>Re: database  free the memory</title>
      <link>https://forums.autodesk.com/t5/net-forum/database-free-the-memory/m-p/7681712#M28614</link>
      <description>&lt;P&gt;Your code remains confusing (you call WblockCloneObjects on the destination database instance whith object ids from the source database).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If I don't misundertand, you already have 50 source files on C: drive (1103440.dwg, 1103441.dwg, ..., 11034449.dwg) and you to create 50 new files on C: (0.dwg, 1.dwg, ..., 49.dwg) and copying the contents of the source file model space into the new file.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If this is not what you want to achieve, please explain what you're trying to do in plain English (use Google transalation if needed) without showing code which does not reflect your goal.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If I completely understood, it looks like copying files and you can simply copy the files without using AutoCAD:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;for (int i = 0; i &amp;lt; 50; i++)
{
    System.IO.File.Copy("C:\\110344" + i + ".dwg", "C:\\" + i + ".dwg");
}&lt;/PRE&gt;
&lt;P&gt;If you absolutely need to make a Wbloc operation (i.e. do not copy the layouts), try the following code and overall compare it to your code to try to understand what was wrong with yours:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;        [CommandMethod("MyMemory")]
        public void myMemory()
        {
            for (int i = 0; i &amp;lt; 50; i++)
            {
                string filename = "C:\\110344" + i + ".dwg";
                Util.PrintLn("file" + i);

                // get the ObjectIds form sourceDb model space
                using (Database sourceDb = new Database(false, true))
                {
                    var sourceObjectIds = new ObjectIdCollection();
                    sourceDb.ReadDwgFile(filename, FileOpenMode.OpenForReadAndAllShare, false, null);
                    using (Transaction extTrans = sourceDb.TransactionManager.StartOpenCloseTransaction())
                    {
                        var extBlockTable = (BlockTable)extTrans.GetObject(sourceDb.BlockTableId, OpenMode.ForRead);
                        var extModelSpace = (BlockTableRecord)extTrans.GetObject(extBlockTable[BlockTableRecord.ModelSpace], OpenMode.ForRead);
                        foreach (ObjectId id in extModelSpace)
                        {
                            sourceObjectIds.Add(id);
                        }
                    }

                    // create a new file and copy the objects
                    string saveFile = "C:\\" + i + ".dwg";
                    using (var destDb = new Database(true, true))
                    {
                        var mapping = new IdMapping();
                        sourceDb.WblockCloneObjects(
                            sourceObjectIds,
                            SymbolUtilityServices.GetBlockModelSpaceId(destDb),
                            mapping,
                            DuplicateRecordCloning.Ignore,
                            false);
                        destDb.SaveAs(saveFile, true, DwgVersion.AC1800, null);
                    }
                }
            }
        }&lt;/PRE&gt;</description>
      <pubDate>Thu, 11 Jan 2018 10:30:39 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/database-free-the-memory/m-p/7681712#M28614</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2018-01-11T10:30:39Z</dc:date>
    </item>
    <item>
      <title>Re: database  free the memory</title>
      <link>https://forums.autodesk.com/t5/net-forum/database-free-the-memory/m-p/7681979#M28615</link>
      <description>&lt;P&gt;With your code (your algorithm more accurately), the memory increase because you add entities to the current database and erase them at each iteration. AutoCAD keeps all these operations in memory.&lt;/P&gt;
&lt;P&gt;The code I purposed creates each new drawing "in memory" as side database and does nothing to the current database.&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jan 2018 12:20:59 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/database-free-the-memory/m-p/7681979#M28615</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2018-01-11T12:20:59Z</dc:date>
    </item>
    <item>
      <title>Re: database  free the memory</title>
      <link>https://forums.autodesk.com/t5/net-forum/database-free-the-memory/m-p/7684222#M28616</link>
      <description>&lt;P&gt;greate &amp;nbsp;! &amp;nbsp; you are right!&lt;/P&gt;&lt;P&gt;that's &amp;nbsp;the code &amp;nbsp;i want &amp;nbsp;,thank &amp;nbsp; you &amp;nbsp;very much!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Besides ,I &amp;nbsp;have a &amp;nbsp;another &amp;nbsp;quesion&lt;/P&gt;&lt;P&gt;you said &amp;nbsp;that "&lt;SPAN&gt;&amp;nbsp;......a Wbloc operation (i.e. do not copy the layouts)",&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I&amp;nbsp;absolutely need to make a Wbloc operation, &amp;nbsp;want &amp;nbsp;copy &amp;nbsp;everything &amp;nbsp;from &amp;nbsp;source &amp;nbsp;fiel &amp;nbsp;,contain &amp;nbsp; layouts.. &amp;nbsp; what &amp;nbsp;should &amp;nbsp;i do ?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 12 Jan 2018 01:52:04 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/database-free-the-memory/m-p/7684222#M28616</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2018-01-12T01:52:04Z</dc:date>
    </item>
    <item>
      <title>Re: database  free the memory</title>
      <link>https://forums.autodesk.com/t5/net-forum/database-free-the-memory/m-p/7684481#M28617</link>
      <description>&lt;P&gt;As I said upper, if you simply want to copy the whole files, you do not need to use AutoCAD.&lt;/P&gt;
&lt;P&gt;Just create a simple Console Application with:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 0; i &amp;lt; 50; i++)
            {
                System.IO.File.Copy("C:\\110344" + i + ".dwg", "C:\\" + i + ".dwg");
            }
        }
    }&lt;/PRE&gt;</description>
      <pubDate>Fri, 12 Jan 2018 05:32:09 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/database-free-the-memory/m-p/7684481#M28617</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2018-01-12T05:32:09Z</dc:date>
    </item>
  </channel>
</rss>

