Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #11  
Old 06-18-2014, 08:25 AM
CoolBlue's Avatar
CoolBlue CoolBlue is offline Slow "comparison/replace" script Windows 7 64bit Slow "comparison/replace" script Office 2013
Advanced Beginner
 
Join Date: Jun 2014
Location: Australia
Posts: 40
CoolBlue is on a distinguished road
Default Slow "comparison/replace" script

Quote:
Originally Posted by whatsup View Post
Hm, no, that's not the right tool to make visible what's stored/remained in memory.

You have to distiguish between "validity" of a variable and it's "presence" in memory. These are different things: After passing e.g. "End Sub" the local variable isn't valid anymore and you can't access it - that's true -. But this doesn't mean necessarily that it isn't still alive within memory.
The watch window rather points out the validity than its presence in memory (though I'm not 100% sure about this).
Yes, I agree and I already mentioned that when I said that using the VBE watch window was probably bogus.

Quote:
Originally Posted by whatsup View Post
To get a glimpse on memory you rather need something like ObjPtr() - which of course itself won't work after leaving the sub for local variables. For example have a look at this:
- Add a module and put the code.
- Add a userform which will be "Userform1"
- Then step through the code by F8 and watch the immediate window
See my comments in blue below explaining my understanding of whats going on.

Quote:
Originally Posted by whatsup View Post
Code:
Public Sub test()
    Dim objForm As Object

'   ///////////////////////////////////////////////////////////////////////////
'   objForm is not yet initialised, it points to Nothing
    Debug.Print ObjPtr(objForm)

'   ///////////////////////////////////////////////////////////////////////////
'   Creates an instance of an Object of type UserForm1 and then 
'   point objForm to that instance.  The object is created implicitly by
'   auto-instantiation.  The object has one reference: it is referenced by objForm.
    Set objForm = UserForm1      
    Debug.Print ObjPtr(objForm)

'   ///////////////////////////////////////////////////////////////////////////
'   Explicitly instantiate a new object of type UserForm1 and then 
'   point objForm to that instance.  Because referencing is one to one or 
'   many to one, but not one to many, objForm is no longer referencing the
'   first object so it's reference count is decreased by one; the first object 
'   now has zero references and will be de-allocated. 
'   The new object has one reference and it is objForm.
    Set objForm = New UserForm1
    Debug.Print ObjPtr(objForm)

'   ///////////////////////////////////////////////////////////////////////////
'   ObjForm is pointed at Nothing, the second object's reference count is
'   decreased by one; it now has zero references and will be de-allocated. 
    Set objForm = Nothing
    Debug.Print ObjPtr(objForm)

'   ///////////////////////////////////////////////////////////////////////////
'   on exit, all local variables are destroyed and any objects referenced by
'   them have their reference count decremented.
'   Any object referenced only by any of these variables will now have zero
'   reference count and will be de-allocated.
End Sub
You will clearly see, that with "New UserForm1" a new entry is created in memory, though it's the same variable. Now, if you don't set it at the end to Nothing, memory keeps occupied, while setting it to nothing the entry in Memory = 0 which signifies it's removed from memory.

Now, I can't proof that this isn't the case if you don't destroy the reference manually but leave it to vba, because I haven't got any idea to read in memory what's in it.
My curiosity drove me to build a test system for this which I have attached below. It uses API calls to report memory used by excel and logs before and after. There are switches on the sheets to direct the various activities. Because its and API report, it is Windows' view of what memory is alocated to Excel so I think that is probably ok and will show any leaks.

There is one for forms and one for the dictionary search. (It was too big as one file...)

The behaviour when set to nothing is used is no different from when its not.
Have a play with it and see what you think...
Attached Files
File Type: xlsm Dictionary Search.xlsm (484.7 KB, 20 views)
File Type: xlsm Form Set To Nothing.xlsm (481.2 KB, 15 views)
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Wierd "script code" in a downloaded .doc file CNBarnes Word 2 10-18-2012 02:07 AM
Slow "comparison/replace" script replace data from variable with "sub and super script" from excel to word by vba krishnaoptif Word VBA 9 06-22-2012 05:08 AM
Slow "comparison/replace" script How to choose a "List" for certain "Heading" from "Modify" tool? Jamal NUMAN Word 2 07-03-2011 03:11 AM
Rules and Alerts: "run a script"? discountvc Outlook 0 06-15-2010 07:36 AM
An "error has occurred in the script on this page" decann Outlook 8 09-03-2009 08:54 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:49 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft