Java Memory Management
All local variables/primitives go on the stack. Every thread has its own stack.
Heap
Heap stores all Objects. So String, Integer etc are created on this. The references to these are given to the Stack.
JAVA is Pass by Value. Every primitive variable created on stack is separate for each function and has a different scope. The output is 5.
The objects reference are passed by value. Hence, a copy of the same memory location is passed to cust and c objects.
Though the output might seem the same as reference but it happens due to a different reason and i.e the objects reference are passed by value.
The final keyword will make sure you cannot change the reference to the variable but one can always change the state/value of the variable if allowed.
Escaping References
When working on projects a bean needs to be immutable. For example if one has a map in bean it needs to be protected from being retrieved directly.
The idea is that when you pass the objects its main copy shouldnt changed.
- One can implement an Iterator interface and so the bean will come iterable. But if the objects inside Map can be changed in value it will not work.
- One can give out a copy of the object in getter. myObj = new ArrayList<> (collectionRef). Again this isn’t fully correct.
If below line is called in calling function
myObj.clear();
And then again bean.getObj(); is used to give us a fresh copy of the object and hence cause confusion as the object isn’t clear.
To give out a copy which cant be modified we can use Collection.unmodifiableMap() etc
- One can make an interface which just gets and leaves out the setter method. The setter method is exclusive to the specialized bean class and we can play with the interface.
- String, Integer, Double etc are immutable so not much to worry. But for the other custom objects one needs to ensure we return only what is needed and not the entire main object.
One can either pass a clone()
Java String Pool
Now one == two because both the strings are placed in the Java String Pool.
However, three!=four as “three” is not placed on the pool directly as it is a calculated String. To place it in the String Pool, we specifically write .intern() at the end of the String.
String pool helps save memory.
Garbage Collection
Eligibility – Any objects that do not have reference to the main stack.
Memory leak – when the object has no reference and it still exists in memory even after garbage collection.
Softmemory leak – An object which has a reference but wont be used ever in the project.
How to identify
Verbose – garbage collection using vmargs
Using Visual VM for profiling
Review the code
Garbage Collection Process
The Mark and Sweep is a two step – Stop the world process
1. Mark all the objects that are on the heap which can be referenced from Stack.
2. Sweep all the objects which are not marked.
3. Ensure that all objects are assigned continuous memory to allow for more memory available for future objects
There is Generational garbage collection which happens in Java .
The process is the same but there is division of memory into young and old. Old is much larger.
Young is Eden Space – Survivor S0 – S1.
New objects are created in Eden then moved to S0 on garbage collection and then again to S1…S7 on subsequent runs.
There can be upto 8 phases depending on the VM and requirement.
If all objects are garbage , then GC ends very quickly as it has been mark very few objects
Identifying Soft Leaks – This tells the old gen is full and nothing is moving to survivor space that means we have some memory leaks.
If you see such a situation then take a heap dump in VisualVm.
Open the Heap dump in the MemoryAnalyser tool .
Now the Shortest Path to the Accumulation Point works in an upside down manner. Java Thread contains Customer Manager – Customer Manager has an ArrayList.
Java 7 vs Java 8
Java 7 – All the classes – meta data and internalized string go into Permgen. Even if you deploy the code the Permgen is not garbage collected and hence keeps on increasing. Therefore server restart is the only way to clear the memory.
Java 8 – PermGen – is moved out of the Heap. It is called Meta Space as it stores only metadata of classes. Internalized strings are moved to old gen. Meta Space is not part of the heap and the memory available to it is taken from System main memory.
There is a concept of Weak and SoftReferences.
Weak References – Will be cleared if GC is run – not guaranteed though
Soft References – Stronger than Weak, will be removed byGC only when the space is needed.
Use: When you want to load a lot of data into memory for a cache etc and want to use garbage collectors ability to collect data one can use this. This way one doesn’t have to manually clear the objects.