Recently, I was writing a static method and a thought of concurrency crossed my mind. So, I read and discussed about this with a colleague and I am sharing my understandings.
Memory in java is split up into two kinds – the heap and the stacks. The heap is where all the objects live and the stacks are where the threads do their work. Each thread has its own stack and can’t access each others stacks.
Each thread also has a pointer into the code which points to the bit of code they’re currently running. When a thread starts running a new method it saves the arguments and local variables in that method on its own stack.
Some of these values might be pointers to objects on the heap.
If two threads are running the same method at the same time they will both have their code pointers pointing at that method and have their own copies of arguments and local variables on their stacks.
They will only interfere with each other if the things on their stacks point to the same objects on the heap.
In which case all sorts of things might happen. But For example Strings are immutable (cannot be changed) so we’re safe if this is the only object being “shared”.
In Summary, While writing static methods try to keep the scope of variables to the smallest possible. this way most probably we are thread safe.
If shared objects are immutable then also we are good to go.
1) objects are created on heap in Java irrespective of there scope e.g. local or member variable. while its worth noting that class variables or static members are created in method area of Java memory space and both heap and method area is shared between different thread.
2) Garbage collection is a mechanism provided by Java Virtual Machine to reclaim heap space from objects which are eligible for Garbage collection.
3) Garbage collection relieves java programmer from memory management which is essential part of C++ programming and gives more time to focus on business logic.
4) Garbage Collection in Java is carried by a daemon thread called Garbage Collector.
5) Before removing an object from memory Garbage collection thread invokes finalize () method of that object and gives an opportunity to perform any sort of cleanup required.
6) You as Java programmer can not force Garbage collection in Java; it will only trigger if JVM thinks it needs a garbage collection based on Java heap size.
7) There are methods like System.gc () and Runtime.gc () which is used to send request of Garbage collection to JVM but it’s not guaranteed that garbage collection will happen.
8) If there is no memory space for creating new object in Heap Java Virtual Machine throws OutOfMemoryError or java.lang.OutOfMemoryError heap space.
When an Object becomes Eligible for Garbage Collection
An Object becomes eligible for Garbage collection if its not reachable from any live threads or any static references in other words you can say that an object becomes eligible for garbage collection if its all references are null.
Cyclic dependencies are not counted as reference so if Object A has reference of object B and object B has reference of Object A and they don’t have any other live reference then both Objects A and B will be eligible for Garbage collection.
Generally an object becomes eligible for garbage collection in Java on following cases:
1) All references of that object explicitly set to null e.g. object = null
2) Object is created inside a block and reference goes out scope once control exit that block.
3) Parent object set to null, if an object holds reference of another object and when you set container object’s reference null, child or contained object automatically becomes eligible for garbage collection.