I believe that there are lots of developers who do not have a computer science degree and did not go from the basic understanding of computers to programming languages like me. I am neither a computer scientist nor a software engineer. Developing web application is totally just my hobby and I self-taught everything except the basic function-based c++ programming style.
I didn't understand the computer memory completely; therefore, when dealing with deep memory allocation problem, i used to just do it based on my feeling to it. A lot of Java developers think that they do not have to deal with memory allocation because of the fantastic garbage collector feature. However, better understanding the garbage collector would help yourself with dipping into the language even more. Some languages that must have a host like Javascript also need the developers to understand memory allocation if you ever consider that speeding up your code is very essential.
Computer memory used for programming is usually divided into 3 parts. Code container (what I call), Stack and Heap. Code container contains the code that your compiler compiles your programming code to. When you program a project, the code is usually in English. However, when the code went through the compiler, it will become machine code.
Stack area in the memory is the place where variables with static size are stored in. Stack follows the Last In First Out rule(LIFO), meaning that the last memory created in the Stack will be removed the first. Usually, those variables are primitive values like int, double, float and so on. (However, when you use primitive wrapper type like in Javascript, you can do "var myVar = new Boolean(true);", this variable will be considered as a reference or pointer pointing to the actual Boolean object which exists in the heap). Because primitive values usually have a static size, they are placed in the Stack area.
Heap area in the memory is the place where variables with dynamic size are stored in. The reason why we call that dynamic size is that size of values in the Heap is allowed to be determined at run time. Usually, with Object Oriented Languages, actual objects are stored in the Heap and object pointers are stored in the Stack. Therefore, when you create two pointers and copy the one pointer to the other one like "obj1* p1 = new Obj(); obj1* p2 = p1" in C++, you are just copying the pointer from p1 to p2 and changes made to the object "p1" points to will directly affect the object that "p2" points to because they are actually pointing to the same object on the Heap.
I love cars just as much as loving web development. The garbage collection feature is good and convenient but just like comparing automatic and manual transmission on the cars, automatic is a convenient feature but it is never as fast as driving the manual one. The garbage collection feature is convenient but it has to search for memory that will not be used then remove it, it makes the process a bit slower.
No comments:
Post a Comment