Optimize J2ME app

zhaozj2021-02-11  186

Author: Eric Giguere compiled: Sean 2002 February 27

If you want to say that J2ME applications and J2SE applications are different, that is their respective environments that are restricted. Many of the main bottlenecks of J2ME systems are the number of available memory stored and running applications. For example, there are many MIDP devices that are limited to the number of memory to the application, only 50K or less, and the server-based J2SE environment that may be required to have a long distance is long. Since you will easily encounter these restrictions in development, in this J2ME technical prompt, you will learn how to make your app to take the least amount of memory. You will use these technologies to reduce the space occupied by MIDlet, and the MIDlet just displays a text box and sounds when the content is changed: package com.j2medeveloper.techtips;

Import javax.microedition.lcdui. *;

Public class beforeesizeoptimization extends Basicmidlet {

Public Static Final Command EXITCOMMAND = New Command ("EXIT", Command.exit, 1);

Public BeforeSizeOptimization () {}

Protected void initmidlet () {getDisplay (). setcurrent (new mainform ());}

Public Class Mainform Extends form {public mainform () {super ("mainform");

Addcommand; Append (TextF);

SetCommandListener (New CommandListener () {public void command (Command C, Displayable D) {if (c == EXITCOMMAND) {exitmidlet ();}}});

SetItemStatelistener (NEW ITEMSTATELISTENER () {public void itemStateChanged (item item) {if (item == textf) {alerttype.info.playsound (getDisplay ());}}})

Private textfield textf = new textfield ("Type Anything", NULL, 20, 0);}}

Although this example only demonstrates the MIDlet, the technique used in it can be used to optimize any J2ME programs. Note that the MIDlet that is demonstrated by the following categories: package com.j2medeveloper.techtips;

Import javax.microedition.lcdui. *; import javax.microedition.midlet. *;

Public Abstract Class Basicmidlet Extends Midlet {

Private display display;

Public BasicMidlet () {}

Protected Void DestroyApp (Boolean Unconditional) THROWS MIDLETSTATECHANGEXCEPTION {EXITMIDLET ();

Public void exitmidlet () {notifydestroyed ();

Public Display getDisplay () {Return Display;

Protected abstract void initmidlet ();

protected void pauseapp () {}

Protected Void Startapp () throws MidletStateChangeException {if (Display == Null) {Display = Display.getDisplay (this); initmIdlet ();}}

} After you pack with J2ME Wireless Toolkit, this MIDlet example is only 4K.

Reducing the first step of SIZE is to remove some unnecessary classes by reducing application functions. Are each characteristic of your app really necessary? Can your users endure without ringtones and whistle? Let us build the smallest version of the app. Note that the MIDlet example is already quite small.

The second step is to focus on the internal class defined by the application, especially anonymous classes. Remember that every class file has a certain space expense. Even the most insignificant classes also need costs: public class foo {// Nothing Here} Compile this class, you can find that you get a Class file that takes up almost 200 bytes. An anonymous class general purpose is used to achieve event monitors. The MIDlet example defines two Listener. A simple optimization is to enable the MIDlet's primary class to implement two interfaces of CommandListener and ItemStateListener, delete the Listener code inside. Remember that multiple objects can use the same Listener. If you need to distinguish it, you can transfer parameters to the CommanAction and ItemStateChanged methods.

The internal class also expansion code through other aspects, as the compiler must generate different variables and methods to allow an internal class to access private information in the package class.

The third step is to maximize the use of built-in classes. For example, don't build your own collection class in a CLDC-based program. Try to use built-in HashTable and Vector. Design MIDP applications are the same. The MIDlet example defines an Form subclass to create its main form, but it can also be easily created directly: mainform = new form ("mainform"); mainform.addcommand (OkCommand); mainform.setcommandlistener (Listener); There is no error here, this is just some simple things that need to be considered. The fourth step is to cancel your application's inheritance relationship. You may have breaking the code into one or more abstract classes, which is a recommended OOD design method that can improve the code reuse. Maybe there is anything you have learned, but to simplify the inheritance hierarchy. If your abstraction class - may come from other projects - just inherited once, it simplifies inheritance relationships. The MIDlet example extends the BAICMIDLET class, but both classes are easy to combine.

The fifth step is to shorten your name, class name, method name, and data member name. It seems that this step is very stupid, but a class file saves a lot of character information. Shorten their names you will reduce the size of the CLAS file. This way to save things will not be a lot, but they will increase when they have multiple classes. The package name should be shortened. Because the MIDP application is completely independent, you can completely avoid the name - will not have the opportunity to conflict with the names of other classes of the device. The MIDlet example can be removed from the com.j2medeveloper.techtips package.

Note that the shorteness name is not what you have to do, we can use a tool called "disturbing". The main purpose of the disturber is to "hide" code "hide" code, which makes the program into something that is almost unable to read when it is compiler. Another role of this process is to reduce the size of the program. The hidden code is mainly named by the hidden code. Here is a disturber of the source code called Retroguard's free tool from http://www.retrological.com, and there are many commercial packages.

Finally, pay attention to the initialization of some arrays. (MIDlet example did not do any array initialization, but this is a very important step) When compiled, an array initializes code like this: int Arr [] = {0, 1, 2, 3}; actually generated code as follows : Arr [0] = 0; Arr [1] = 1; Arr [2] = 2; Arr [3] = 3; If you want to study, you can use Java2SDK to decompose the byte code into a Class's Java. Tool (using the -c parameter). You may be surprised and dissatisfied with the results you see. Two optional methods are (1) encoding the data into a string, decoded to an array, or (2) saving the data into the package into the package and accesses the GetReourceAsStream method with Class Loader when running.

What you say here is just some guidance, and it is not mentioned here for every J2ME application. However, most of them can apply this MIDlet example. The optimized version of the MIDlet is as follows: import javax.microedition.lcdui. *; Import javax.microedition.midlet. *;

Public Class Aso Extends Midlet imports commandListener, itemStatelistener {

Private Display Display; private form mainform; private textfield mainformtf = new textfield ("type anything", null, 20, 0);

Public Static Final Command EXITCOMMAND = New Command ("EXIT", Command.exit, 1);

Public ASO () {}

Public void CommandAction (Command C, Displayable D) {if (c == exitcommand) {exitmidlet ();}}

Protected Void DestroyApp (Boolean Unconditional) THROWS MIDLETSTATECHANGEXCEPTION {EXITMIDLET ();

Public void exitmidlet () {notifydestroyed ();

Public Display getDisplay () {Return Display;

Protected void initmidlet () {mainform = new form ("mainform"); mainform.addcommand; mainform.setCommandListener (this); Mainform.SetItemStateListener (this); MainForm.Append (MainFormTF);

GetDisplay (). setcurrent (mainform);

Public void itemStateChanged (item item) {if (item == mainformTF) {alerttype.info.playsound (getDisplay ());}}

protected void pauseapp () {}

Protected Void Startapp () throws midletStateChangeException {if (display == null) {display = display.getdisplay (this); initmidlet ();}}}

Original: http://wireless.java.sun.com/midp/ttips/appsize/

转载请注明原文地址:https://www.9cbs.com/read-5788.html

New Post(0)