Micro blogpost: Unmanaged memory in GC enabled programming languages
Disclaimer: This article is based on my current knowledge and understanding. If you have any query or see any improvement, please mention in the comment section
Languages like C, C++ teaches programmers to allocate their own memory and free it once done whereas languages like Java, C# and Golang don’t put this responsibility on programmers. These languages have GC to take care of memory management. Due to automatic memory management, we have limited ability to control when GC is triggered. Due to which if GC triggers automatically while the app is performing a task then it can affect performance of the app.
Languages like Java and C# give the ability to programmers to allocate unmanaged memory where GC won’t play a role and performance doesn’t get affected.
Below is one such example. Task here is to create and initialise an integer array with values, sum all elements of the array and repeat this step for 5 times. When using unmanaged memory ( nativememoryarray) we can see from method m2 statistics that extremely less memory is allocated compared to the managed memory array of method m1.
Note : To create native memory array, I have used following library - https://github.com/Cysharp/NativeMemoryArray
~ Happy Coding