Java - Object lifecycle and memory management
-- a. ------------------------------
In this program Object obj is created inside for loop
so its scope is limited to only for loop, and only for
the one iteration of the for loop.
for each iteration new object is created, and after i
increments to i++, that object become useless hence it is
collected by JVM garbage collector.
so at any time only one object obj is exists in memory so
memory leak or out of memory error cannot occur.
here while loop uses true so it will run infinitely
without errors.
-- end of a. -----------------------
-- b. ------------------------------
In this program also while loop is infinite.
inside while List of objects is created, but its scope is
only
limited to while loop, so for each while loop iteration new List is
created
discarding previous one.
Inside for loop 1000000 new Objects are added to the list.
thread sleeps
and one while loop iteration is completed, so the list becomes
useless and
collected by garbage collector. and all process repeats.
so for each iteration of while, the List will have only 1000000
elements
at maximum, and in normal situations handling 1000000 elements can
be handled easily
So memory leak or out of memory error cannot occur.
-- end of b. -----------------------
-- c. ------------------------------
In the third program List is created before while loop, so its
scope is at main() method level
so it stays in memory until main completes.
But here while loop is true/ infinite so main never
returns/completes
and list stays in memory forever till the program is terminated
inside while loop 1000000 elements are added to the list EACH
time for loop runs,
so list grows continuously
list keeps growing and its size also growing
In java, object's data is stored in heap memory, so it grows in
this program
and eventually runs out of memory
finally memory leak occurs in the form of OutOfMemoryError
Exception.
error time depends on the available memory/RAM in system
more the ram, more it will take to be full
-- end of c. -----------------------
Hope this helps, do comment if having any issue. thank you
//c. sample program MemLoadTest2.java
import java.util.*;
public class MemLoadTest2 {
public static void main(String[] args) throws Exception {
List<Object> list = new ArrayList();
while(true) {
System.out.println("List size:" + list.size()); //to check the
growing size of list
for (int i=0;i<1000000;i++) {
list.add(new Object());
}
Thread.sleep(10); // wait it set to 10 for faster output
}
}
}
/* output
List size:0
List size:1000000
List size:2000000
List size:3000000
List size:4000000
List size:5000000
List size:6000000
List size:7000000
List size:8000000
List size:9000000
List size:10000000
List size:11000000
List size:12000000
List size:13000000
List size:14000000
List size:15000000
List size:16000000
List size:17000000
List size:18000000
List size:19000000
List size:20000000
List size:21000000
List size:22000000
List size:23000000
List size:24000000
List size:25000000
List size:26000000
List size:27000000
List size:28000000
List size:29000000
List size:30000000
List size:31000000
Exception in thread "main" java.lang.OutOfMemoryError: Java heap
space
at java.util.Arrays.copyOf(Unknown Source)
at java.util.Arrays.copyOf(Unknown Source)
at java.util.ArrayList.grow(Unknown Source)
at java.util.ArrayList.ensureExplicitCapacity(Unknown Source)
at java.util.ArrayList.ensureCapacityInternal(Unknown Source)
at java.util.ArrayList.add(Unknown Source)
at MemLoadTest2.main(MemLoadTest2.java:10)
*/
Java - Object lifecycle and memory management Consider the following Java programs: Program Code publ ic cl ass A a pub...