JVM non-deterministic elapsed times?

The garbage collections routines need to run some time, even if you don't have garbage to collect.

The garbage collections routines need to run some time, even if you don't have garbage to collect. It is easy to assume that since you don't allocate or dereference memory then you will not need to collect garbage; however, the garbage collection routine is running in an independent thread and does not inspect the possible flows through your code to determine if it should run. So, the garbage collection routine launches and finds no garbage to collect.

That takes some time. In addition, your program (that is, the entire JVM interpreting your classes) may have been swapped off the CPU by the operating system's need to immediately handle some interrupt. This can even happen in multi-core systems, depending on the CPU's core selection algorithms.

Examples of items that the CPU must handle immediately include copying Ethernet memory buffers into system memory (to prevent dropped packets), capturing keyboard input, etc. In short, if you want your analysis to mean something, you have to do real statistics on your benchmarking, as all sorts of external items can impact your running program's time.

Incidentally, this points out one reason to use System.gc() even though people seem to start religious wars about not using it. I have seen System.gc() cause my program to be less efficient (efficiency is work divided by elapsed time). However, if you need semi-consistent execution times, you absolutely must use System.gc() but use it when you know for certain there is a benefit in the way of consistency.My program has nothing to do 95% of the time so I don't care about efficiency.

– broiyan Oct 23 at 5:50 If you really want consistent execution times, you don't call System.gc() a lot. You pre-allocate all the objects you need, and then do a lot of object copying (instead of instantiation). By requesting garbage collection (you didn't think that System.gc() actually forced garbage collection, did you?

) you might run the garbage collector more often, but you certainly don't guarantee it. Nor do you guarantee that it will do less work for each run. The only thing you might be able to guarantee is that after doing it's work it will likely free fewer objects.

– Edwin Buck Oct 24 at 7:00 1 "Nor do you guarantee that it will do less work for each run. " By this I think you mean that I cannot get better consistency (less garbage collection work each and every run; less meaning less than the worst case run in the default implementation where there is not liberal use of System.gc()). If in fact this is what you mean, to my surprise you are right.

Now I'm back to square 1 trying to get consistency. – broiyan Oct 27 at 11:52.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions