An application has to process a lot of data in approximately 50,000 drawings, collect information from every drawing with a recursive function that calls itself for every subdirectory, looking for DWG files in the directory, something similar to this blog post Batch process in-memory.
After processing the DWG file, the Database variable gets out of scope and the
destructor is called. This should release all memory associated with this instance of Database. However, the destructor does not release all memory assigned to the instance of Database. When processing all these drawings, about 50 MB memory is lost. Is it possible to release all the memory or is there a memory leak in AutoCAD?
This is a general problem of the C++ memory manager, that also happens to .NET languages, and happens due to memory fragmentation, and it probably cannot be avoided.
If an (internal) AutoCAD function needs memory, AutoCAD asks the operating
system for it. Normally, if the function has finished, AutoCAD releases the used
memory and the memory manager gives it back to the operating system. The problem is that the memory manager can release the memory (gives it back to the
operating system) only if it is not fragmented.
Let’s say the function ‘a’ allocates 1 MB, and another function ‘b’ allocates 100 KB. The operating system assigns a 1.1 MB memory block to the application. If the function ‘a’ releases its 1 MB, the memory manager cannot give the memory back to the operating system because there is still 100 KB in use. The memory manager marks the 1 MB as free. If a third function ‘c’ allocates now less than 1 MB, the memory manager knows that it has already 1 MB of free memory and assigns the needed memory to the calling function. It does not ask the operating system for more memory. If function ‘b’ now releases its 100 KB, the memory manager cannot give the memory back to the operating system, because function ‘c’ uses now memory from the original 1,1 MB memory block.
If function ‘c’ allocates 500 KB, there is still 500 KB available. If the first function now needs 1 MB again, the memory manager needs to allocate a new memory block from the operating system (with a size of 1 MB; it’s not possible to get 500 KB from one memory block and the other 500 KB from a second memory block).
To release all memory you have to quit AutoCAD. Therefore, it is safe to say that it is not possible to read up to 50,000 drawing files in one session without incurring any memory loss.
There are only two possible work-arounds. The first is to increase the amount of
physical RAM installed on the computer. The second one is to close AutoCAD after
processing some files. Restart AutoCAD and use your functions again. You could
write your current application status into a file. When restarting, you could analyze your previous application state and continue from it.
Starting on 2013, AutoCAD can run on console mode, which makes easier to restart the process and therefore better use release the memory.

Leave a Reply to Umesh WorlikarCancel reply