VB.Net - Excel Interop - Passing Excel.Range ByVal versus ByRef: Impact on memory usage for very large ranges
I'm confused about how much memory an Excel Range object actually takes up, especially if the range points to a large block of cells in a worksheet.
I'm working with some VB.Net code that automates Excel using Interop and have noticed that Excel ranges are always passed ByVal and I was wondering if I could conserve a significant amount of RAM at run-time if I were to pass them ByRef.
We're dealing with some pretty big blocks of data these days (200,000+ rows of 10 to 15 columns) and running into situations where users are reporting out of memory errors (although we're having trouble re-creating them)
So here's my question:
If an Excel range object points to a large block of cells (say 150,000 rows by 20 columns), would I save a lot of memory at run-time by passing ranges around in my code by reference instead of by value?
I understand the implications of ByRef in that the called routine can manipulate the original object directly. From what I've seen of the code, none of these routines need to change the object anyway (probably why they were using ByVal in the first place).
When you pass in an object, the object is always passed in by reference. In the case of an object parameter, the difference between ByVAl and ByREf, determines how the reference is passed in.
So...
if you pass in an object reference as ByVal, then you are sending a copy of the reference, and your reference can be "repointed" to another instance of that type of object, and your original reference will remain unchanged.
If you pass in an object reference as ByRef, then when you change what object the reference points to, that change will also be reflected in the calling routine.
In Summary whether you use byRef or ByVal, you are not going to see much of a performance impact either way.
you can see the following article for more details on the topic of how ByRef vs ByVal are treated.
this might help in what you are wanting to do in excel and vb.net.
I worked on a project a few years ago that was giving me problems with working in excel spreadsheets and vb.net.
Problem 1. Excel Interop does not work in 64 bit and in 64 bit servers.
Problem 2. Need a software that does not need microsoft office software.
So here is the answer that solved all my problems and more.
.NET doesn't know anything about referencing counting, so the System.Runtime.InteropServices.Marshal class has the ReleaseComObject method to decrement the reference counter, and release memory.
Thanks guys. I did not realize that all objects are passed by reference. That makes sense, as does the difference between passing them byref and byval.
Interesting reading on ReleaseCOMObject. I have run into those types of problems implementing the Excel application event handler for a COM Add-in. I had to implement my own handler as the one built in to VB.net was not releasing its COM objects properly.
0
Question has a verified solution.
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.
One of a set of tools we're offering as a way of saying thank you for being a part of the community.
So...
if you pass in an object reference as ByVal, then you are sending a copy of the reference, and your reference can be "repointed" to another instance of that type of object, and your original reference will remain unchanged.
If you pass in an object reference as ByRef, then when you change what object the reference points to, that change will also be reflected in the calling routine.
In Summary whether you use byRef or ByVal, you are not going to see much of a performance impact either way.
you can see the following article for more details on the topic of how ByRef vs ByVal are treated.
http://www.cpearson.com/excel/byrefbyval.aspx