sw2013 will automatically release the memory (model view) when the component documents are closed or the main assembly. I believe that sw2012 does the same. You can try unloading an addin, to force it but that may or may not work.
Michael thanks for the quick reply. If SolidWorks is releasing the memory, then I don't know why the memory gets up to around the 8GB mark. The codes I am using to close all the documents is:
Is there a different line of code I need to use, or should this one release the memory even though it doesn't seem to.
Try closing the assembly through the UI. Is this any different? Unload an addin. Does this have an affect? Otherwise, there is no way to force it.
If I close the assembly through the UI, the memory goes down but not to its original size. I did a test with SolidWorks and got the following results:
- Closed SolidWorks and made sure the applications and processes for SolidWorks didn't exist in the Task Manager
- Opened a Fresh instance of SolidWorks - 109,524 K for the SolidWorks Process Memory
- Opened an assembly - 320,784 K for the SolidWorks Process Memory
- Closed the assembly - 234,252 K for the SolidWorks Process Memory
- Reopened the assembly - 371,116 K for the SolidWorks Process Memory
- Closed the assembly - 316,700 K for the SolidWorks Process Memory
The more I do this, the more memory SolidWorks hogs up. Is there a way to clear this with an API call or something to clear the memory?
There's no API call to do that. As I stated, unloading an addin might help, but it's incidental.
Ok, thank you for all of your help
In case anyone comes upon this thread, here is some code that I edited from this thread to get the job done for my program:
Make sure to include: Imports System and Imports System.Threading at the beginning of your module.
Here is the code I inserted at the end of my loop. That way once i hits any multiple of 100 in my loop, it closes SolidWorks and clears the memory:
' Code to reset memory in SolidWorks after every 100 models made
Dim ResetNum As Double
ResetNum = i / 100
If ResetNum = CInt(ResetNum) Then
swApp = Nothing
' Sleep for 5 seconds
Catch ex As Exception
Dim p As Process
For Each p In System.Diagnostics.Process.GetProcessesByName("SldWorks")
' Sleep for five seconds
At the beginning of my loop I have the following code. With this at the beginning of the loop, it will restart SolidWorks if it was closed by the program, or continue to use the open instance.
swApp = CreateObject("SldWorks.Application")
swApp.UserControl = True
swApp.Visible = True
This would be a more graceful way to kill the SolidWorks process:
Dim p As Process() = System.Diagnostics.Process.GetProcessesByName("SldWorks")
I had a similar problem. I needed to remove 'virtual' components of an assymble. To do that I select the components, remove thme from the assembly and then try to delete them using the File.Delete (VB) method. Before deleting the files I used CloseDoc to close them.The problem was that even after closing them, SW seems to keep the files opened ; the File.Delete throwed an execption telling me that the file is used an cannot be deleted. Even if I tried to delete the files from Windows Explorer the result was the same : 'file used'.
So I solved the problem by clearing the assembly selection list and undo list (calling ModelDoc2.ClearSelection2(True) and ModelDoc2.ClearUndoList()). After doing those calls the SldWorks.CloseDoc has closed correctly the files and I was able to delete them.
I hope this will help.