Slashdot Mirror


Initializing all Java classes at Start-Up

Jean-Marie Dautelle writes "Java classes are initialized at first-use only which can introduce significant runtime delays detrimental to real-time or games applications (for which accurate scheduling is often required). To solve this problem, the latest open-source Javolution library supports initialization of all Java classes at start-up (e.g. javolution.lang.ClassInitializer.initializeAll(); // Initialize runtime classes (rt.jar) and all classes in classpath). Note: Runtime class initialization (rt.jar) takes typically a few seconds and about 3 Mbytes of memory."

56 comments

  1. Re:And? by Anonymous Coward · · Score: 0

    How long would it take someone to write this themselves if they needed it? 10 minutes?

  2. Wow by thesnarky1 · · Score: 1
    A whopping 3 MB of memory... eh, what the heck, I'll even give 'im 5 'cause I'm so generous!

    On a serious note, This is interesting. I program in java, some games in fact, and I think I'll be checking this one out, just to see if/what an impact it makes.

  3. tumbleweed? by Cronky · · Score: 4, Funny

    system.tumbleWeed.initialise() ?

  4. Benefits for anything other than games? by Carl+Rosenberger · · Score: 2, Interesting

    "A few seconds" and 3MB of memory for rt.jar sound nice but I wonder how timings and memory consumption would look like with a couple of more libraries in the CLASSPATH, how about a full Eclipse environment?

    The problem with Java startup times is that every Java app loads it's own complete runtime environment. This is not easy to fix, since many apps depend on the fact that they get their own.

    1. Re:Benefits for anything other than games? by breadbot · · Score: 4, Interesting

      Sun has been talking about shared VMs for a while now -- I can't remember if it's scheduled for version 6 or 7 -- the object space would be partitioned among the running Java "processes", but only one JVM would be necessary. It would improve startup times, memory consumption, etc. They could all share one instance of rt.jar, for example, assuming it was process-safe (it may not be now, but fixing it would be something Sun would have to do for the actual release). I don't see it listed in the Java 6 (Mustang) notes (for example here), so I would guess that it's slated for versin 7 (Dolphin).

    2. Re:Benefits for anything other than games? by ear1grey · · Score: 1

      There are benefits outside of gaming, for example, when testing applications, especially distributed ones, a random bit of latency can be the difference between a pass and a fail, and can also be a total bastard to debug, so being able to minimise the unexpected is a welcome tool.

      Regardless of this, the garbage collector can always throw a spanner in somewhere, so it's a moot point.

    3. Re:Benefits for anything other than games? by RingDev · · Score: 1

      "It would improve startup times, memory consumption, etc."

      Only on the 2nd and later instances. The initial java application startup would still be slow.

      -Rick

      --
      "Most people in the U.S. wouldn't know they live in a tyrannical state if it walked up and grabbed their junk." - MyFirs
    4. Re:Benefits for anything other than games? by ultranova · · Score: 1

      There are benefits outside of gaming, for example, when testing applications, especially distributed ones, a random bit of latency can be the difference between a pass and a fail, and can also be a total bastard to debug, so being able to minimise the unexpected is a welcome tool.

      During testing, wouldn't you want the application to fail if it at all can, to find bugs ? Wouldn't adding random latency help the testing process ?

      Or what did you mean ?

      --

      Forget magic. Any technology distinguishable from divine power is insufficiently advanced.

    5. Re:Benefits for anything other than games? by erlenic · · Score: 1

      Yes, you do want it to fail due to programming errors, but not due to random latency in the VM. Granted, if your application is suffering from that latency you have a problem, but not an easy one to find or fix. If they can get rid of these random errors, then it will be easier to find and fix the real problems.

    6. Re:Benefits for anything other than games? by ultranova · · Score: 1

      Yes, you do want it to fail due to programming errors, but not due to random latency in the VM.

      If it fails due to random latency in the VM, then it has a programming error. After all, no multitasking system can possibly guarantee certain latencies - the system could have a higher-priority thread running just when your thread needs to be run. Or some piece of data or code you need might be swapped out (altought that can be prevented by locking it in memory). Or something else might go wrong.

      Any program that depends on certain timings for correctness on a platform that's not specifically designed for them is fundamentally broken.

      --

      Forget magic. Any technology distinguishable from divine power is insufficiently advanced.

    7. Re:Benefits for anything other than games? by ear1grey · · Score: 1
      During testing, wouldn't you want the application to fail if it at all can, to find bugs ?

      In most cases, yes; 'though testing usually just proves compliance to an agreed set of interfaces and service levels, the fact that the testing process finds bugs is an agreeable side effect.

      Also the JVM and it's contents may not be under test, they may be part of the test harness.

      Wouldn't adding random latency help the testing process ?

      When testing, we minimise the unknown and then measure one thing at a time. The random latency caused by class loading and the aforementioned garbage collection are unknowns, so controlling them is a benefit. That's not to say that testing with the "real" latency is not a good thing if it's likely to exist in the real environment, it's just that being able to control exactly when it is allowed into the mix makes the overall test process more robust.

    8. Re:Benefits for anything other than games? by Hard_Code · · Score: 1

      It took me forever to find (first documentation which made no mention of the flag, then 'strings' on the client 'jvm.dll' and then google searching which turned up nothing except a single usenet post on the issue which lead me back to, guess what, the docs where it was hidden somewhere other than the tool docs), but anyway, this is partly implemented in Java. 1.5:

      http://java.sun.com/j2se/1.5.0/docs/guide/vm/class -data-sharing.html

      Basically the VM slurps up the runtime library, preparses it, and dumps it back in a fashion which it can memory-map much faster on subsequent launches.

      --

      It's 10 PM. Do you know if you're un-American?
  5. Is this newsworthy? by dascandy · · Score: 0

    Making a function that calls System.XYZ.Init() for each xyz is newsworthy?

    Egad... Time to submit my hello world-programs...

    1. Re:Is this newsworthy? by Threni · · Score: 3, Funny

      >Making a function that calls System.XYZ.Init() for each xyz is newsworthy?
      >Egad... Time to submit my hello world-programs...

      My hello world program takes a array of char to describe which world is being greeted. The version under development will accept an array of pointers to strings so that more than one world can be greeted.

    2. Re:Is this newsworthy? by dascandy · · Score: 1

      You're going to have to do more than that to compete with GNU Hello...

  6. Firefox problem by Threni · · Score: 0, Offtopic

    After e.g. and before ). There's a bunch of text that Firefox isn't rendering properly on my PC. Any ideas what I can do about it? I've seen it elsewhere, and fiddling with my fonts in the menu options didn't help.

    1. Re:Firefox problem by Threni · · Score: 1

      Fixed it. You can't use `courier` as a font for Monospace, although `courier new` is fine.

    2. Re:Firefox problem by Arandir · · Score: 1

      Why can't you use "courier"? Why can't people use font tags correctly? Stop specifying exact fonts!

      Don't use {font-family: "Courier New"}, instead use {font-family: "Courier New", "Courier", "monospace"}. No matter how common the font, never NEVER assume that the user has it.

      Please see http://www.w3.org/TR/REC-CSS1#font-family

      --
      A Government Is a Body of People, Usually Notably Ungoverned
  7. Re: How long would it take to write this? by Carl+Rosenberger · · Score: 3, Informative

    How long would it take someone to write this themselves if they needed it? 10 minutes?

    Maybe a day ?

    (1) Identify all files and directories that can be found in the CLASSPATH.
    (2) Look at the directories and add all contained files to the list of files.
    (3) Differentiate beween types of files:
    (4) Add .class files to the list of class names
    (5) Look through all zip format files (.jar, .zip, .war ... ) and find all class names in there.
    (6) Call ClassLoader#loadClass with all classnames.

    (5) will take longest to write yourself, but there should certainly already be code out there tht gets that done

    (6) Which ClassLoader to use? Surely the ClassLoader needs to be supplyable as a parameter.

  8. Re: How long would it take to write this? by billcopc · · Score: 1

    I haven't touched Java with a 10-foot pole in over 5 years, but wouldn't this be something you do with the Reflection functions to get the list of defined classes and have yourself some Foreach fun.

    --
    -Billco, Fnarg.com
  9. Very, very rarely a good idea by /ASCII · · Score: 4, Interesting
    There are two reasons why lazy initialization is almost always the better solution:
    • Not all classes are used. Why pay a memory and performance penalty for having e.g. the Java2d API installed if your application never uses it?
    • Startup time will suffer. A 3 second startup penalty for only the bare minimum runtime, and probably much more for any non-trivial application is a huge deal.

    The advantage of doing eager initialization is predictability, and in the case where almost all classes get initialized sooner or later (which would be very rare considering the size of the Java API) a slight performance increase.

    Maybe some kinds of games would benefit from this, but almost all other applications would benefit from more lazy initialization, not less.
    --
    Try out fish, the friendly interactive shell.
    1. Re:Very, very rarely a good idea by hey! · · Score: 1

      Well, keep classes in a priority queue. Time spent initializing classes after the post-startup phase is used to prioritize the classes, and set a budget of so many MB. If the 80/20 rule holds, you get most of the benefit for a fraction of the cost.

      When a new class rises to the level of the weakest member of the startup herd, it takes its place. Over time the system will coverge to optimal behavior given the resources you're willing to devote to this purpose, adjusting as the usage of the computer changes.

      --
      Post may contain irony: discontinue use if experiencing mood swings, nausea or elevated blood pressure.
    2. Re:Very, very rarely a good idea by Arandir · · Score: 1

      There are two reasons why lazy initialization is almost always the better solution

      Of course. No one is questioning this. But the blurb says it is doing this for real-time and game applications. These are the exceptions and not the norm, but are no less valid because they are not the norm.

      Most dynamically typed languages suck like a Hoover when it comes to realtime, so this is a solution that may help.

      --
      A Government Is a Body of People, Usually Notably Ungoverned
  10. Shameless Plug or Valid Selfpromotion? by dorkygeek · · Score: 5, Interesting

    Let's see. Submitter of story: Jean-Marie Dautelle. Javolution project owner: Jean-Marie Dautelle.

    --
    Windows is like decaf - it tastes like the real thing, but it won't get you through the day.
    1. Re:Shameless Plug or Valid Selfpromotion? by d-rock · · Score: 2, Interesting

      Well, Jean-Marie *has* contributed a lot to the Java community. While this particular post may look trivial or not super important, the Javolution framework does have a lot of nice features and is RTSJ (real-time Java) compliant. He's also contributed a lot to JScience, which is another interesting framework for scientific calculations.

      BTW, a lot of realtime Java has to do with deterministic behavior, and lazy initialization is non-deterministic. I don't think the main point here is to save a little bit of time by doing the load up front, but rather to get deterministic behavior from the system later on. For instance, let's say I have a class that normally take 20us for the constructor to run, but takes 15ms to load the class. If I'm expecting to be able to allocate a new instance of this class inside a method that has a maximum run time of 100us, then the system will fail if lazy initialization takes place. If that means I just missed a whole bunch of sensor readings, or that motor ran longer than it was supposed to, it could have some very bad consequences.

      If you're interesting in learning about real time systems, or about real time Java in particular, I highly recommend the NIST's report on requirements for real time Java:

      http://www.itl.nist.gov/div897/ctg/real-time/intro .html

      Derek

      --
      Don't Panic...
  11. Read before commenting by Anonymous Coward · · Score: 0
    Note the first sentence:

    Java classes are initialized at first-use only which can introduce significant runtime delays detrimental to real-time or games applications

  12. Great code quality by tcopeland · · Score: 1

    At least in terms of unused code. I ran PMD's unused code rules on it and found no problems. Good stuff!

    1. Re:Great code quality by avalys · · Score: 1

      Give me a break. All that means is that someone ran the same (or similar) analysis against the code, and deleted everything it complained about.

      At best, I suppose it suggests that the programmer may be thorough and methodical, and I'll grant that those are good attributes for a programmer to have. But it says nothing about security, efficiency, expandability, reusability, and so forth. Certainly, not enough to proclaim "Great code quality".

      --
      This space intentionally left blank.
    2. Re:Great code quality by hiero · · Score: 1

      A good IDE, like Eclipse, will visually show you any unused code. No need to run another tool.

    3. Re:Great code quality by MntlChaos · · Score: 1

      an IDE will show you unreachable code, not code that is never reached...

      int foo(int x)
      {
          if(x <= 2) return 0;
          int f = factorial(x);
          if(isprime(f){return -1;}
          return 1;
      }


      That never returns -1, but a compiler can't know that

    4. Re:Great code quality by flibuste · · Score: 1

      And your point with this code?

      With this particular example, it's absolutely certain that if any analysing tool tells you every control path is covered, then it's lying. It cannot tell you however that this code will never be reached, unless you run test cases for an infinite number of numbers, which is hardly achievable in a life time.

      Unfortunately your example is flawed and demonstrates the contrary of what you apparently want to say. However it's a great example to show that tools are just what they are...tools, and thus cannot be relied upon for a definitive answer. Humans are still needed to review code.

  13. Re: How long would it take to write this? by Carl+Rosenberger · · Score: 1

    No, reflection won't work to load all classes in the CLASSPATH. For reflection you would need a starting point from where all other classes are referenced. There is no common root. Any line of code can decide to create an instance of a class that has not been used or referenced yet.

  14. Getting to the root of it by mparaz · · Score: 1

    If you want to dictate how the JVM starts up at a fundamental level, go hack on Apache Harmony, the open source Java implementation. They're just getting started so your contribution could go a long way.

  15. Speding up Java Load Times by Sweep+The+Leg · · Score: 1, Interesting

    I speed up my java load times by using this tecnique that I like to call C++.

    On a more serious note, one of the problems with load times I have noticed in a lot of Java is that people unnecessarily include namespaces where they don't need/use them. Sometimes there's no reason to go into another library when there's an equivalent function in one you're already using and there's certainly no need to reference things you don't use. That also creates a design problem in addition to the performance issue IMO because although it's not hurting too bad, it's a violation of least privilege.

    This post reminds me of some other related problems that occur in other languages both design and performance related. Although the issue with .NET is somewhat different, the design part is still the same. It gets worse in VB.NET (please don't start flaming) where people can have project level imports. It even bothered me in C++ when someone would lets say remove functionality but leave all the imports/#define behind. Today I came accross this problem in ASP.NET an webpage:

    imports System.Web.UI (only namespace used)
    imports System.Text (not used!)
    imports System.XML.Serialization (not used!)
    imports System.Threading (not used!)

    1. Re:Speding up Java Load Times by GuyWithLag · · Score: 1

      I don't want to sound adversarial or anything, but please get a clue first...

      The only time that excess namespaces delay processing is during the compilation phase (you know, the *.java -> *.class process). In fact, the class files do not have the concept of import statements, as all class identifiers are fully expanded.

      (this applies only to java, I have no experience on the .Net side of things.)

    2. Re:Speding up Java Load Times by Sweep+The+Leg · · Score: 0

      Unfortunately I think you misunderstood my first comment so you also need to get a clue with all due respect. Perhaps I should have been more clear.

      Namespace 1: used all over
      Namespace 2: 1 function call that could be substituted with call in namespace for function 1.

      This commonly props up when using 3rd party library. 3rd party library contains all you need, but then person decides to add yet another import to call something they didn't realize was in 3rd party library. result-> bloat -> load time.

    3. Re:Speding up Java Load Times by dorkygeek · · Score: 1

      Aah, and this can't happen in C++ how?

      --
      Windows is like decaf - it tastes like the real thing, but it won't get you through the day.
    4. Re:Speding up Java Load Times by GuyWithLag · · Score: 1
      The original point you made was

      On a more serious note, one of the problems with load times I have noticed in a lot of Java is that people unnecessarily include namespaces where they don't need/use them.

      for which my comment applies.

      On the next sentence you talk about the problems Java programmers have when they use many large libraries that have a functionality overlap (correct?), which is a problem, and is unrelated to the first...
    5. Re:Speding up Java Load Times by Sweep+The+Leg · · Score: 0

      No, you still misunderstand. Again, probably from bad writing on my part. I am saying as a result of people using overlapping functionality, load time increases. If you use an extra namespace, load time will increase. Period. Not sure why we are debating this.

  16. Java "import" considered harmful by metamatic · · Score: 1

    Yes, "import" is a problem. It's such a PITA to identify the actual bits you need, that most code just imports huge chunks of the API.

    Furthermore, "import" has no dependency handling. For instance, org.xml.sax.XMLReader is no use without org.xml.sax.helpers.XMLReaderFactory. Rather than have to manually hunt down the dependencies in the few functions they use, most programmers import org.xml.sax.*.

    In C/C++/Objective-C, at least include files tend to include their dependencies.

    --
    GCHQ Quantum Insert installed. If only our tongues were made of glass, how much more careful we would be when we speak
    1. Re:Java "import" considered harmful by xornor · · Score: 1

      try using eclipse

    2. Re:Java "import" considered harmful by metamatic · · Score: 1

      I don't want to learn a new text editor--and a horrendously complicated one at that--just to write Java programs.

      --
      GCHQ Quantum Insert installed. If only our tongues were made of glass, how much more careful we would be when we speak
    3. Re:Java "import" considered harmful by Anonymous Coward · · Score: 0

      I really hope you saw this comment.

      The "import" declaration has no counterpart in the resulting compiled code. It is just a convenience for the programmer, because typing fully qualified class names is a PITA. But the compiler will generate the same code (classes are always fully qualified) no matter whether class references are fully/simply qualified and no matter how many/what packages you import.

      I agree though that Type-Import-on-Demand declarations make it harder for the reader to figure out where the classes being used actually come from.

    4. Re:Java "import" considered harmful by metamatic · · Score: 1

      "a convenience for the programmer"? This must be some strange new usage of the word "convenience"...

      --
      GCHQ Quantum Insert installed. If only our tongues were made of glass, how much more careful we would be when we speak
    5. Re:Java "import" considered harmful by Anonymous Coward · · Score: 0

      Then use emacs. It also knows how to generate imports.

    6. Re:Java "import" considered harmful by metamatic · · Score: 1

      If the import can be generated automatically, WHAT IS THE POINT OF IT? It doesn't do me any good, and it apparently doesn't do the compiler any good.

      --
      GCHQ Quantum Insert installed. If only our tongues were made of glass, how much more careful we would be when we speak
  17. Ancient proverb by Rufus88 · · Score: 1

    [...] supports initialization of [...] all classes in classpath

    "He who packs his entire wardrobe for a row across the lake becomes the best-dressed fish around." (Source unknown)

  18. Yeah, but you wouldn't notice it by Julian+Morrison · · Score: 1

    "javad" would boot from /etc/init.d and be running by the time KDE got finished loading the kitchen sink. At which point, Java processes would have as low startup overhead as regular ones.

    1. Re:Yeah, but you wouldn't notice it by RingDev · · Score: 1

      And for the vast majority of computer users who are not on KDE...

      -Rick

      --
      "Most people in the U.S. wouldn't know they live in a tyrannical state if it walked up and grabbed their junk." - MyFirs
    2. Re:Yeah, but you wouldn't notice it by Anonymous Coward · · Score: 0

      ...you'd do the same thing in their own specific way? duh?

    3. Re:Yeah, but you wouldn't notice it by Anonymous Coward · · Score: 0

      And double the boot time every day, just because about once every month one needs to go to a webpage that absolutely wants Java? No thanks.

  19. Re: How long would it take to write this? by lexarius · · Score: 1

    Unfortunately there just isn't a good way to determine what classes are available. Reflection doesn't have a way to determine all available subclasses of a given class, which would be really useful.

  20. Re: How long would it take to write this? by 42forty-two42 · · Score: 1

    There's a library class for accessing ZIP/JAR files. All in all it sounds rather simple.

  21. Wow... tumbleweeds... by SteeldrivingJon · · Score: 1

    I guess Java really is a ghost town these days. Everyone rode the rails on out of town.

    --
    September 2011: Looking for Cocoa/iOS work in Boston area Cocoa Programmer Quincy, MA