×

Discussion Board

Page 1 of 2 12 LastLast
Results 1 to 15 of 28
  1. #1
    Regular Contributor
    Join Date
    Jun 2009
    Location
    Tel-Aviv Israel
    Posts
    410

    Loading time take too long... On real device

    hey, I trying to reduce the loading time of my application, I reduced images amount by combining a few file together, and it made a small difference, I'll get to the rest of it in a while, but first i must know how java loads the classes into the memory, does java at startup loads all the classes to the memory and perform all the static blocks, or does it loads the class to the memory on the first instance of the class?

    I wonder since i have a few static components i might build differently...
    Last edited by TacB0sS; 2009-08-11 at 00:15.

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Loading time take too long... On real device

    There is a description of the class loading process in this wiki article.

    It's worth avoiding anything time consuming in static initializer code, since it can be hard to control exactly when that code will run. "Time consuming" code is loading images or other data from the JAR, reading or writing RMS, HttpConnection, and so on.

    Graham.

  3. #3
    Regular Contributor
    Join Date
    Jan 2008
    Posts
    387

    Re: Loading time take too long...

    Hi, first of all optimize your code, second optimize png images, use a thread to load images, and in foreground display something like loading....etc.
    Thanks and Regards,
    Manjul.

  4. #4
    Regular Contributor
    Join Date
    Jun 2009
    Location
    Tel-Aviv Israel
    Posts
    410

    Re: Loading time take too long...

    hey, after a long while of optimizing of my application, which included:

    • Removing all unnecessary static object creation.
    • I designed an object to hold all the information of the specific images, and load the images only on need.
    • I designed an object (ComponentGenerator) that may be added to a container and only when it receive focus from a focus Manager, and it calls a method
      public Component generateObject();
      and it replaces the ComponentGenerator with the generated Component.


    my point is nothing loads until its time for it to load. yet still on the Motorola E1000 phone takes it 50 sec to start up, and a few object takes a while to load.

    • I have many classes which i think takes a long time to load.
    • I guess the device is old and newer devices would perform better.
    • most of my design involves inner classes and inner inner classes some static some not, could that be slowing down my application?
    • some of my code design involves abstract classes that I create new instances of them and implement abstract method in them, could that be slowing down my application?

    except for these could there be any other reason I did not think about?

    and one more thing, after loading the application perform perfectly, no delays no animation stuck or jump every thing works fine but loading.

    does the wtk come with an external plugin to view start up time information?

    I could really use a good advice.

    Thanks,
    Adam.
    Last edited by TacB0sS; 2009-08-16 at 10:39.

  5. #5
    Regular Contributor
    Join Date
    Jun 2009
    Location
    Tel-Aviv Israel
    Posts
    410

    Re: Loading time take too long...

    hey i used the method tracker in the wtk and all it keep writing in the console is:

    Code:
    Invoking method 'java/io/InputStreamReader::read()I'
    Invoking method 'java/io/InputStreamReader::ensureOpen()V'
    Leaving java/io/InputStreamReader::ensureOpen
    Invoking method 'java/io/Reader::read()I'
    Invoking method 'com/sun/cldc/i18n/j2me/UTF_8_Reader::read([CII)I'
    Invoking method 'com/sun/cldc/i18n/j2me/UTF_8_Reader::getByteOfCurrentChar(I)I'
    Invoking method 'com/sun/midp/midletsuite/ManifestProperties$UTF8ManifestStream::read()I'
    Invoking method 'java/io/ByteArrayInputStream::read()I'
    Leaving java/io/ByteArrayInputStream::read
    Leaving com/sun/midp/midletsuite/ManifestProperties$UTF8ManifestStream::read
    Leaving com/sun/cldc/i18n/j2me/UTF_8_Reader::getByteOfCurrentChar
    Invoking method 'com/sun/cldc/i18n/j2me/UTF_8_Reader::prepareForNextChar()V'
    Leaving com/sun/cldc/i18n/j2me/UTF_8_Reader::prepareForNextChar
    Invoking method 'com/sun/mmedia/QSoundRenderThread::nRender(I)I'
    Leaving com/sun/mmedia/QSoundRenderThread::nRender
    Invoking method 'java/lang/Thread::sleep(J)V'
    Leaving java/lang/Thread::sleep
    Leaving com/sun/cldc/i18n/j2me/UTF_8_Reader::read
    Leaving java/io/Reader::read
    Leaving java/io/InputStreamReader::read
    and nothing else for a very log while, i lost my patient after 2 minutes of it writing it to my console.
    it does not say what invokes the read() method anywhere there where too many repeats of this text exactly one after the other with nothing in between.

  6. #6
    Regular Contributor
    Join Date
    Jun 2009
    Location
    Tel-Aviv Israel
    Posts
    410

    Re: Loading time take too long...

    Ok, I'll ask a different questions,

    1. How many classes should a respectable application hold?
    2. Does newer phones load classes quicker?
    3. Could i mask the loading time of the classes with a progress bar?
    4. Is there any kind of tool, to help me combine objects, except Ctrl+C & Ctrl+V?
    5. Is there a way to cause other class files which were not loaded yet to load before the VM decide its their time to load?


    A simple yes or no would do.

    Thanks,
    Adam.

  7. #7
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Loading time take too long...

    Sorry, I didn't notice your posts yesterday.

    For game development, I'd usually try to keep the class count down as low as possible. Two classes is the minimum, and many games have around five. However, this is to keep the space down, and I haven't experienced specific load-time problems with higher class counts.

    There's no rule that newer phones are faster.

    This wiki article describes the process of class loading, and should tell you enough to have some control over when the classes are loaded.

    It is possible to merge classes. See The Class Stacking Technique. However, class merging relies either on an expensive tool (mBooster), or on a specific class structure.

    I'd still favour something that's happening in the code rather than the class count. The number of classes might simply be making it difficult to work out which bits of code are running at which time, especially where class initializer code is concerned.

    Are there many threads? How do you load resources from the JAR (other than createImage())? Text, tile maps, anything like that.

    Graham.

  8. #8
    Regular Contributor
    Join Date
    Jun 2009
    Location
    Tel-Aviv Israel
    Posts
    410

    Re: Loading time take too long...

    if to be honest, I don't have much text to load so it is all still in the code, I'll change that later when the application would be complete.

    I only load images and i use this class to load them on the first call to their images:

    PHP Code:
        public static class ImageCollection {
            private 
    boolean loaded;
            private 
    String collectionName;
            private 
    Image[] images;
            private 
    int height;
            private 
    int width;
            private 
    String path;
            private 
    ImageCollection(String collectionName,final String path,final int width,final int height) {
                
    this.collectionName collectionName;
                
    this.path=path;
                
    smartImagesLoader.add(this);
                
    this.width=width;
                
    this.height=height;
            }
            public 
    Image[] getImages() {
                if(
    images==null)
                    
    loadImages();
                return 
    images;
            }
            public 
    void loadImages() {
                if(!
    loaded) {
                    
    images=ImageLoader.loadImages(path,width,height);
                    
    smartImagesLoader.remove(ImageCollection.this);
                }
            }
            public 
    String getName() {
                return 
    collectionName;
            }
            public 
    int getWidth() {
                return 
    width;
            }
            public 
    int getHeight() {
                return 
    height;
            }
        } 
    and this is the thread to load them in the background:
    PHP Code:
        public static class SmartImagesLoader implements Runnable {
            private 
    Vector actionCollections=new Vector();
            private 
    Object monitor;
            private 
    boolean pause;
            public 
    void add(ImageCollection collection){
                
    actionCollections.addElement(collection);
            }

            public 
    void remove(ImageCollection imageCollection) {
                
    actionCollections.removeElement(imageCollection);
            }

            public 
    void run() {
                while(
    actionCollections.size()>0) {
                    if(
    pause) {
                        
    synchronized(monitor) {
                            try {
                                
    monitor.wait();
                            } catch (
    InterruptedException e) {
                                
    e.printStackTrace();
                            }
                        }    
                    }
                    ((
    ImageCollection)actionCollections.elementAt(0)).loadImages();
                }
            }
            
            public 
    void pause(Object monitor) {
                
    this.monitor=monitor;
                
    pause=true;
            }
            public 
    void resume() {
                
    monitor.notify();
                
    pause=false;
            }
        } 
    and no there is only the main thread for update and repainting, and an occasional thread for loading menus and other stuff.

    BTW I read the article thats how i figured out my slow loading time is because I have too many classes...
    Last edited by TacB0sS; 2009-08-18 at 12:50.

  9. #9
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Loading time take too long...

    Hmmm... I can't say that it's a cause of your problems, but I have some worries about this.

    First, I'm guessing that you're adding to a queue on one thread, and processing it on another. There's no synchronization on the collection, which could result in the processing loop believing that the queue is empty when it is not.

    Second, "pause()". If this was called between "synchronized(monitor)" and "monitor.wait()", then the code would attempt to wait on an object for which it doesn't hold a lock (because "monitor" will point to a different object). This would result in the loading process stalling, unless the thread is re-started later on.

    It's possible that some threading issue like these might be causing the code to take longer than it should. A change of VM can often cause threading problems to come to light, in code that worked fine on other VMs.

    Try replacing this with something very simple (no queue, no thread), and see if that affects the start up time. If not, we might have to look at what happens in the ImageLoader class?

    Graham.

  10. #10
    Regular Contributor
    Join Date
    Jun 2009
    Location
    Tel-Aviv Israel
    Posts
    410

    Talking Re: Loading time take too long...

    well thank you for your enlightenment, I have avoided all the race conditions possible:


    PHP Code:
        private final void loadImages() {
                if(!
    loaded) {
                    
    loaded=true;    //This should do the trick 
                
    images=ImageLoader.loadImages(path,width,height);
                    
    smartImagesLoader.remove(ImageCollection.this);
                }
            } 
    and BTW i did not start the thread to load all the images, perhaps I should... lol

  11. #11
    Regular Contributor
    Join Date
    Jun 2009
    Location
    Tel-Aviv Israel
    Posts
    410

    Re: Loading time take too long...

    Thank you for your help so far graham...
    i was just wondering:

    if I have 10 classes in 10 different files

    or 10 classes in one file

    does this matter in loading time, I mean I can have one file hold tons of static classes as inner classes

    so I guess the question is what takes longer to load and link the class or to open and close the class file itself?

  12. #12
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Loading time take too long...

    How many source files makes no difference. Each class becomes a separate .class file once compiled.

    If you use inner classes, you are likely also to get extra .class files, small classes with names ending "$n". Some of these will be anonymous inner classes, but some will be synthetic classes, which are used to help connect inner classes to their enclosing classes. In some cases, you can find that every one of your inner classes ends up with one of these "companion" classes.

    Large numbers of inner classes may make it harder to control the load-time of classes, since you'll have many inter-connected classes.

    Inner classes are very easy to over-use. I have to make a conscious effort to avoid them. I'm not suggesting that inner classes are bad, just that they're easy to use... inappropriately.

    I'd put each class in a separate file, but just because it makes it easier to navigate in eclipse.

    When it comes to class merging (or otherwise reducing the number of .class files)... yes, I'd guess that fewer, larger classes will load faster than more, smaller classes. But I don't have any metrics to suggest how much difference it would make.

    Interfaces can slow things down. If you have an interface with only one implementing class, Proguard should be able to optimize the interface out of the application.

    Classes that contain only static final variables, initialized from literals, will also be removed by Proguard. But they won't get loaded even if they're there, so this won't affect loading time.

    Hmmmm... maybe a large number of inner classes is the problem...?

    Graham.

  13. #13
    Regular Contributor
    Join Date
    Jun 2009
    Location
    Tel-Aviv Israel
    Posts
    410

    Question Re: Loading time take too long...

    I'll check if inner classes make a difference, I'll put many of my classes in one file and see what happens on the phone...
    will report back...

    Adam.

  14. #14
    Regular Contributor
    Join Date
    Jun 2009
    Location
    Tel-Aviv Israel
    Posts
    410

    Re: Loading time take too long...

    does not make a difference

  15. #15
    Regular Contributor
    Join Date
    Jun 2009
    Location
    Tel-Aviv Israel
    Posts
    410

    Unhappy Re: Loading time take too long...

    i have to say programming this way goes against everything I thought and believed to be the "Right way to write a program"

    to write a game in even 20 classes is really not OOD, the only thing it is, is a mess, you can't make heads or tail of you objects, that terrible... I mean how do you handle such a mess??

Similar Threads

  1. MBM image is not found
    By tamhanna in forum Symbian
    Replies: 14
    Last Post: 2009-04-20, 12:44
  2. Game takes a long time to load.....
    By ziyaaf in forum Mobile Java General
    Replies: 4
    Last Post: 2008-06-19, 10:12
  3. Time of loading a dll via ECOM
    By xhsoldier in forum Symbian
    Replies: 0
    Last Post: 2008-05-29, 08:52
  4. the real UTC ( Universal Time )
    By telephia in forum Symbian
    Replies: 5
    Last Post: 2007-05-16, 18:10

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×