*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Performance comparison w.r.t memory on Loop variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Performance comparison w.r.t memory on Loop variables" Watch "Performance comparison w.r.t memory on Loop variables" New topic
Author

Performance comparison w.r.t memory on Loop variables

Muhammad Yasir Ch
Greenhorn

Joined: Oct 05, 2007
Posts: 27
I like to know the performance comparison w.r.t memory between following code snippets. According to my knowledge the first code snippet will consume more memory as compared to second one.

Code1
for (long l = 0; l <= 1000000; l++) {
ArrayList<myBean> arrBeanList = getBeanList();
}

Code2
ArrayList<myBean> arrBeanList = null;
for (long l = 0; l <= 1000000; l++) {
arrBeanList = getBeanList();
}


SCJP 6- 86%
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

The answer cannot be straight forward IMO. Code 1 creates more reference variables (arrBeanList) but they also become unreachable after each iteration. Thus the object returned by getBeanList() will be eligible after each iteration. Code 2 reuses the same reference variable arrBeanList but after the last iteration the object returned by getBeanList() will not be directly eligible for GC (if there is more code to follow)...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Performance comparison w.r.t memory on Loop variables
 
Similar Threads
arrayList.clear() VERSUS arrayList = new ArrayList()
Row Actions on DataTable
Autoboxing / unboxing
What`s the problem?