class MyNetPack {
Long count;
public MyNetPack() {
count = Long.valueOf(0);
}
public void reinit(Long count) {
this.count = count;
}
public void process() {
/* Some calculation */
}
}
public class MyWork {
public static void main(String[] args) {
MyNetPack my = new MyNetPack();
for (long i = 1; i < 100000000; i++) {
my.reinit(i);
my.process();
}
}
}
I'm creating single object by using
MyNetPack my=new MyNetPack();
Afterwards reusing the same object with the reinit method as follows,
for (long i = 1; i < 100000000; i++) {
my.reinit(i);
my.process();
}
Please explain the initial memory allocation & reuse of memory in stack and heap level.
From my understanding,
MyNetPack reference holder will be allocated in stack and Object will be allocated in heap (with reference holder for count) . Each time in the for loop, The actual value of count (say 1,2,3..) will be allocated newly in heap and the reference will be placed in MyNetPack->count reference holder.
Guide me to minimize new object & memory allocation..
Thanks Joseph