More efficient memory deallocation

C

ct60

Hello again,

I have code which involves many objects that have references to each other.

This causes large memory leaks since VBA is not able to deaalocate all
memory that has gone out of scope. So, I set all references, etc to NOTHING
when the objects terminate. Problem is there is still a significant memory
leak AND it is quite time consuming.

So does anyone know of a fast way to deallocate memory? The Unload
statement does not seemto help either.

What might be ideal is maybe a windows API function that deallocates memory.
In theory we should be able to point to an address in memory and a size of
the object structure and free that memory. Does anyone have know the details
of we can do something like that?

Any insights would be greatly appreciated.

Thanks,

Chris (ct60)
 
T

Tom Ogilvy

VBA releases memory when it is appropriate. There are certainly bugs that
cause memory leaks, but I haven't heard of any associated with assigning
variables.
 
P

Peter T

From what I've read, rather than what I know, the consensus seems to be it's
generally un-necessary to explicitly set object references to nothing just
before they would lose scope automatically, and might even do harm if done
in the wrong order. A strong and noteworthy opponent against is Matthew
Curland.

What seems more contentious though is the subject of "circular references",
even as to how the term is defined. From what you've described perhaps you
have those.

There has been a lot of discussion over the years in
microsoft.public.vb.general.discussion

Concerning API's you could look at HeapAlloc & HeapFree, though I doubt they
will help and I wouldn't touch them without fully understanding them.

I trust you are not using the End statement at all. Any automation, if so
are created objects closed/quit before any references to them are
destroyed..

Regards,
Peter T
 
C

ct60

Thanks Guys -

I believe that this problem is fairly well documented.

If you have object A which has a ref to obj B which has a ref to obj C ( and
lets say that obj C also has a ref to obj B), when A goes out of scope - in
theory B and C should also. This does not happen. Watch in the memory usage
of excel in the task manager to see this effect.

Explicit use of the set obj=Nothing before terminating will resolve this
situation to a certain extent. Again, checking the memory usage for larger
programs will show clearly the memory deallocation. Out testing shows,
however, that there is still a fairly significant memory leak and the process
is time consuming.

One would expect that memory deallocation should be a very high speed
process. That being said, one of the advantages of using vba is that the
machine is smart enough to take on memory management issues rather than the
programmer.

And all this works ok until those rare time when we get into highly
sophisticated programs and want to have greater control over memory
management.

I am sure there are ways to get handles to objects in memory and dealloc
them in a high speed pprocess but it is surely a slippery slope. If this
problem is too pronounced then maybe we need lower level languages like C/C++
or perhaps C#.

Thanks again.

Chris
 
P

Peter T

I believe that this problem is fairly well documented.

And highly debated!

In theory although memory may not be released when the reference goes out of
scope while the app is still running, when the code fully terminates VB(A)'s
garbage collection "should" restore. At least that's what some say.

A working example perhaps along the lines of what you describe is argued
here
http://tinyurl.com/vl624

(see thread view post 8 & on)

I suspect memory leak may also be caused by other things besides object
references.

Regards,
Peter T

http://tinyurl.com/vl624
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top