'Pro'gramming Tips
Memory Allocations!
Value Type ??? & Reference Type ????
Yes, Of Course ??
This Article is continuation to "The Legacy of Memory Management - 1".
Let's dive in...
Hold On!!! let us know few takeaways of this article to maintain curiosity??
- What Are treated as Value types & Reference types?
- Where do locate in Memory?
- How do they Differ in memory life cycle?
- What is the cost of their usage?
As name says Value type variable holds the value with it. Most Basic value types are, all numeric data types(Int, Double, ... ), Enum, Struct, Bool & Char.
- Value type's memory is located in stack (Stack is contiguous memory allocated to run the app in RAM - it can also be referred as Stack Registers) When a function is called, a new block of allocation will be pushed to stack for local variables and executing statements. When that function exits, the block becomes unused and popped from stack.
- Out of all Value types, Struct has separate fan base! as it has similar features of Class (Reference type).
- As ????explained with struct Biryani, a struct can hold values of value types and reference types.
- Value type object returned the clone of the same object upon assigning to a new variable.
- Read & Write operations are efficient in speed, as the Stack is a contiguous memory allocation space.
- Keep struct as small as possible to limit the memory usage in Stack, to make it more efficient.
- A Struct can hold the reference type as a variable, but releasing of the referenced object doesn't happen when Struct pops from Stack, it should be taken care by developer (Not needed if IDE supports Garbage collector or Auto releasing) else it will trigger Memory leak for referenced object.
- A Struct cannot be inherited from other structure or class.
Reference type doesn't hold the value, instead it holds the memory location of the value. It's memory is located in heap (Heap is an open space of memory, not contiguous. There is no specific rule to allocate memory, it is taken care by MMU).
- Use Reference type if the object is globally used.
- Accessing Reference type's memory is much slower compared to Value type, as reference type need to look in memory location first, value next in Heap.
- When there is a need of holding huge number of variables or data types, create them under Class instead of Struct, It helps to manage the object in Heap, reducing the load on Stack.
- Again ??, it's Biryani but this time it is a Class, hence the result is.
- The way it differs with value type can be noticed ????while assigning & updating the variables of Biryani's instances. Both instances point to memory location of Biryani object. Hence changing value of any instance will reflect in other instances.
- As mentioned in Value type's tips, releasing of Reference type's Memory should be taken care by developer explicitly, when there is a cyclic dependency with in same object (IDE takes care of releasing unused memory allocations), else they will create terrible trouble Memory Leaks
After All...
Biryani should be Tasty!!! ??
Thanks for the read, Will share more updates to get rid of memory leaks in next article.
Happy Coding????
Other Articles: