如何释放对象并清除VBA中的内存 [英] How to release an object and clear memory in VBA

查看:4094
本文介绍了如何释放对象并清除VBA中的内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

那是我第一次发布并添加一个业余程序员,所以让我知道您是否需要任何其他信息.我有以下问题:

thats the first time i post plus im an amateur programmer, so let me know if you need any additional information. i have the following problem:

使用excel VBA,我连接到另一个程序(即Aspen EDR).为此,我安装了相应的加载项.要访问Aspen EDR,我需要添加一个对象.即时通讯完成后,我想释放对象以节省一些内存.我尝试的第一件事是这样:

with excel VBA i connect to another programm (namely Aspen EDR). For that purpose I have an according Add-In installed. to access Aspen EDR i need to add an object. after im done i want to release the object to save some memory. first thing i tried is this:

Dim ObjEDR      As BJACApp
Dim Path        As String

Path = 'assume this is the correct path to the file i want to open
Set ObjEDR = New BJACApp ' Create the BJAC object

If Not ObjEDR.FileOpen(Path) Then
    MsgBox "Can't open file!"
End If

'...

Set ObjEDR = Nothing

我的问题是:在我未设置任何对象之后,excel不会释放内存(正如我在任务管理器中看到的那样).当然,经过几百次迭代(我必须打开很多这样的文件),我得到一条错误消息,即excel内存不足. 我读了一些线程,显然没有什么只能删除对对象的某种引用,但不能删除对象本身,因此我尝试添加fileclose

My Problem is: after i set the object nothing, excel does not release the memory (as i can see in my task manager). of course after a few hundred iterations (i have to open a lot of these files) i get an error message, that excel is out of memory. i read a few threads and apparently nothing only deletes some kind of reference to the object but not the object itself, so i tried adding fileclose

'...

ObjEDR.FileClose
Set ObjEDR = Nothing

当执行FileClose时,我可以看到释放了一点内存(3 MB的0,5),但是仍然有很多内存在累积.

when executing the FileClose i can see that a little memory is released (0,5 of 3MB) but still there is a lot of memory accumulating.

感谢您的帮助:)

推荐答案

删除New关键字,不需要它.

Remove the New keyword, there is no need in it.

Dim ObjEDR      As BJACApp
Dim Path        As String

Path = 'assume this is the correct path to the file i want to open
Set ObjEDR = BJACApp ' Create the BJAC object

If Not ObjEDR.FileOpen(Path) Then
    MsgBox "Can't open file!"
End If

'...

ObjEDR.FileClose
Set ObjEDR = Nothing

这篇关于如何释放对象并清除VBA中的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆