Thursday, May 01, 2008

Memory Usage in Java

Memory management in Java is not a thing to worry about, the Java Virtual Machine and the garbage collector handle it. However, when dealing with obfuscated codes, being aware of where things are stored in the memory is an advantage.


There are two logical places in the memory, the stack and the heap. Local variables, local reference variables and method invocations reside in the stack while instance variables, instance reference variables and objects reside in the heap.


Local variables as the name suggests are defined inside a method or as parameters of a method. Local reference variables on the other hand are those that refer to an object. These are defined inside a method or as parameters of a method. Method calls are pushed on to the stack.


Instance variables are primitive variables defined inside a class but outside of any method. Instance reference variables are those that refer to objects and are defined inside a class but outside of any method. Objects are representations of real-world entities that the program is trying to solve.

No comments: