This is the mail archive of the java@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: heap allocation and gc with gcj


Vladimir Leven wrote:

Using GCJ (3.2.2) If I do the following in a loop:

while (true) {
   byte[] bytes = new byte[1024];
   //do some unrelated stuff
   bytes = null;
}

Will the array be allocated each time the loop runs or will the compiler remove it because it is not used in the method?

No, GCJ is not yet smart enough to do this sort of optimization. It shouldn't be too difficult to add, however, with the tree-ssa framework. But, we tend to assume that developers are smart enough to not write such code - there are other areas where smarter optimizations by the compiler should yield broader benefits - type and bounds check elimination, for example.


What about if it is a public instance variable?

If its a public variable, the opportunities to optimize are much more limited. You could eliminate the allocation only if there are no calls to external methods, unless you can prove that every method you call could never try to access that variable. The existence of synchronization may also effect this -


Note that an easy way to determine if gcj implements a certain optimization is often to "gcj -O2 -S File.java" and look at the asm output.

Regards

Bryce



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]