best practices to optimize memory in C#,
- Create only the objects as and when needed
- Decide the scope for each variable and object, if they are required inside methods declare them inside those methods, do not make them
private
- Use the
IDisposable
interfaces on your custom objects and release all the resources(if any), un-register from all the events etc
- Call
dispose
when an object is no long required for your custom objects
- Use least
static
variables or instances, if required then too think twice whether those objects are required in entire lifetime of program
- Do not use
GC.Collect()
manually,(it is bad practice)