After finishing use the varible, can I clear the memory early before the GC do it?
If there are specific resources that need to be released immediately, implement the IDisposable
interface and call Dispose()
(sometimes Close()
on some objects, such as streams).
If you are looking to prevent passwords being retained in memory beyond their lifespan, SecureString
supports this, though it is not simple to use.
Otherwise, you have no control over when the garbage collector runs or what it actually does. If you desperately need this kind of control, you need a lower level language.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With