This is a basic Question for all programming languages, But I am much familiar with VBA.So Im using the vba code. I got some doubt with deallocating the memory.Does the performance degarde when we erase or swap off the allocated contents ?
something like
Method I where we deallocate the allocated memory
sub my_function()
Dim a() as string
redim a(1 to 15)
'some for loop goes here aroung 50 iterations
erase a ' Deallocate the array
' some piece of code goes here may be 40 lines
end sub
Method II where we dont deallocate any memory
sub my_function()
Dim a() as string
redim a(1 to 15)
'some for loop goes here aroung 50 iterations
'some piece of code goes here may be 40 lines
end sub
Method III where we dellocate memory at the last statment
sub my_function()
Dim a() as string
redim a(1 to 15)
'some for loop goes here aroung 50 iterations
'some piece of code goes here may be 40 lines
erase a 'deallocate the memory
end sub
The question
Method I is erase a, Does it eat up any huge cycles ? or more bytes of ram ? or should i wait for the end of the sub routine or should I go for using erase statement
Method II There is no erase statement in the sub routine, the function kills the array at the end of sub routine but I dont need the array till the end of function so is this fast enough or optimized?
Method III At the first watch, people say its a foolish thing to dellocate at the end of the sub routine as itself kills at the end,But I am eager to know Thanks
The answer will somewhat depend on the language used and how large the memory being used is.
For VBA in 99.99% of all the cases I can imagine there is no performance gain from deallocateing array memory, and its probably marginally slower.
There are rare cases in VBA where it is worthwhile explicitly destroying objects.
For languages such as C/C++ that do not automatically handle memory deallocation it is important to explicitly program memory deallocation so that your code does not contain a memory leak.