![]() |
When to kill an object
Hi there,
I have a general question on what's best practise for killing redundant objects. For example, I've been using 'Set objRng = Nothing' at the end of various procedures on the assumption that this clears them from memory. So............. a) Is that what happens? b) Are variables/objects cleared when a workbook is closed, the application, or on re-boot the whole PC!? Thanks in advance John |
When to kill an object
Setting an object to nothing when you are done with it is never a bad idea.
That being said VB is (if you believe the glossy brochure) self cleaning and will destroy any objects when they are done. That is to say when the sub procedure ends. All of this assumes that the objects are declared locally and not globally. Local variables ar stored on the stack which is destroyed when procedures end, where as global variables are stored on the heap which exists for so long as the project is active. I myself don't specifically clean up locally declared workbook, worksheet or range objects (unless they are instantiated multiple times in a loop). My thought are what is the worst that can happen if they don't get cleaned up. How much memory is being lost as a result. In they typical application not very much. The one exception to this is if I declare objects which are links to other programs such as ADO record sets or instantiations of other programs like Word. Then I am always very careful to clean up after myself. -- HTH... Jim Thomlinson "John" wrote: Hi there, I have a general question on what's best practise for killing redundant objects. For example, I've been using 'Set objRng = Nothing' at the end of various procedures on the assumption that this clears them from memory. So............. a) Is that what happens? b) Are variables/objects cleared when a workbook is closed, the application, or on re-boot the whole PC!? Thanks in advance John |
When to kill an object
Jim,
That's great. Thanks very much for your expertise. I shall rest easy on my "locals" and take more care with cross app code as you suggest. Are you able to point me towards a good background on memory management? I've heard of heap and stack, but don't really understand where they sit in the big scheme of things and, perhaps I ought to.......bedtime reading perhaps! Anyway thanks very much for your help. Best regards John "Jim Thomlinson" wrote in message ... Setting an object to nothing when you are done with it is never a bad idea. That being said VB is (if you believe the glossy brochure) self cleaning and will destroy any objects when they are done. That is to say when the sub procedure ends. All of this assumes that the objects are declared locally and not globally. Local variables ar stored on the stack which is destroyed when procedures end, where as global variables are stored on the heap which exists for so long as the project is active. I myself don't specifically clean up locally declared workbook, worksheet or range objects (unless they are instantiated multiple times in a loop). My thought are what is the worst that can happen if they don't get cleaned up. How much memory is being lost as a result. In they typical application not very much. The one exception to this is if I declare objects which are links to other programs such as ADO record sets or instantiations of other programs like Word. Then I am always very careful to clean up after myself. -- HTH... Jim Thomlinson "John" wrote: Hi there, I have a general question on what's best practise for killing redundant objects. For example, I've been using 'Set objRng = Nothing' at the end of various procedures on the assumption that this clears them from memory. So............. a) Is that what happens? b) Are variables/objects cleared when a workbook is closed, the application, or on re-boot the whole PC!? Thanks in advance John |
When to kill an object
I took a couple of courses in C and learned more about memory than you can
shake a stick at. That of course does not help you. As for books... Not too sure. -- HTH... Jim Thomlinson "John" wrote: Jim, That's great. Thanks very much for your expertise. I shall rest easy on my "locals" and take more care with cross app code as you suggest. Are you able to point me towards a good background on memory management? I've heard of heap and stack, but don't really understand where they sit in the big scheme of things and, perhaps I ought to.......bedtime reading perhaps! Anyway thanks very much for your help. Best regards John "Jim Thomlinson" wrote in message ... Setting an object to nothing when you are done with it is never a bad idea. That being said VB is (if you believe the glossy brochure) self cleaning and will destroy any objects when they are done. That is to say when the sub procedure ends. All of this assumes that the objects are declared locally and not globally. Local variables ar stored on the stack which is destroyed when procedures end, where as global variables are stored on the heap which exists for so long as the project is active. I myself don't specifically clean up locally declared workbook, worksheet or range objects (unless they are instantiated multiple times in a loop). My thought are what is the worst that can happen if they don't get cleaned up. How much memory is being lost as a result. In they typical application not very much. The one exception to this is if I declare objects which are links to other programs such as ADO record sets or instantiations of other programs like Word. Then I am always very careful to clean up after myself. -- HTH... Jim Thomlinson "John" wrote: Hi there, I have a general question on what's best practise for killing redundant objects. For example, I've been using 'Set objRng = Nothing' at the end of various procedures on the assumption that this clears them from memory. So............. a) Is that what happens? b) Are variables/objects cleared when a workbook is closed, the application, or on re-boot the whole PC!? Thanks in advance John |
When to kill an object
Well maybe a "book" is more than I need at the moment, but thanks anyway
Jim. I've since come across a nice article (for anyone who's interested) which explains the "heap" and "stack" parts at a level I'm able to understand! It's really for VB.Net but I assume this principles are the same: http://www.devcity.net/Articles/79/1...cal_oop_1.aspx Best regards John "Jim Thomlinson" wrote in message ... I took a couple of courses in C and learned more about memory than you can shake a stick at. That of course does not help you. As for books... Not too sure. -- HTH... Jim Thomlinson "John" wrote: Jim, That's great. Thanks very much for your expertise. I shall rest easy on my "locals" and take more care with cross app code as you suggest. Are you able to point me towards a good background on memory management? I've heard of heap and stack, but don't really understand where they sit in the big scheme of things and, perhaps I ought to.......bedtime reading perhaps! Anyway thanks very much for your help. Best regards John "Jim Thomlinson" wrote in message ... Setting an object to nothing when you are done with it is never a bad idea. That being said VB is (if you believe the glossy brochure) self cleaning and will destroy any objects when they are done. That is to say when the sub procedure ends. All of this assumes that the objects are declared locally and not globally. Local variables ar stored on the stack which is destroyed when procedures end, where as global variables are stored on the heap which exists for so long as the project is active. I myself don't specifically clean up locally declared workbook, worksheet or range objects (unless they are instantiated multiple times in a loop). My thought are what is the worst that can happen if they don't get cleaned up. How much memory is being lost as a result. In they typical application not very much. The one exception to this is if I declare objects which are links to other programs such as ADO record sets or instantiations of other programs like Word. Then I am always very careful to clean up after myself. -- HTH... Jim Thomlinson "John" wrote: Hi there, I have a general question on what's best practise for killing redundant objects. For example, I've been using 'Set objRng = Nothing' at the end of various procedures on the assumption that this clears them from memory. So............. a) Is that what happens? b) Are variables/objects cleared when a workbook is closed, the application, or on re-boot the whole PC!? Thanks in advance John |
All times are GMT +1. The time now is 11:58 PM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com