In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to optimize the J2ME program size". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to optimize the J2ME program size".
If there is one biggest difference between j2me and j2se, it is that they run in different environments. The main limitation of J2ME is that the memory space it can use to store data and run programs is too small. At present, most devices that support MIDP limit applications to 50K, which means that gigabit J2ME services run in very different environments. Next we will learn some techniques to minimize J2ME programs. Here is an example of minimizing program size:
XML:namespace prefix = o ns = "urn:schemas-microsoft-com:Office:office" / >
Package com.j2medeveloper.techtips
Import Javax.microedition.lcdui.*
Public class BeforeSizeOptimization 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 (exitCommand)
Append (textf)
SetCommandListener (new CommandListener () {
Public void commandAction (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 MIDlet is only used as an example here, the size optimization techniques used can be applied to any J2ME's profile.
Note that the above MIDlet class requires the following helper classes:
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 MIDletStateChangeException {
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 ()
}
}
}
If you run this example with J2ME Wireless Toolkit, the size of the program is about 4K.
To better illustrate what to do, we list the things you need to pay attention to to reduce the size of the program:
1, remove unnecessary classes to ensure the simplicity of the program structure. Have you ever considered that all the features are really needed for your program? Can your users get something simpler? With this in mind, your program is ready for minimization.
2, the second step is to take a closer look at the internal classes defined by the program, especially anonymous classes. Remember, each class file has a certain amount of overhead associated with it. Even the most common classes have overhead.
Public class foo {
/ / nothing here
}
To compile the Class class file, you need 200 bytes size, plus the implementation of some general methods of this class, such as implementing the Event Listener interface. And itself a MIDlet needs to implement interfaces such as CommandListener and ItemStateListener, if you can, you can use them in a file, multiple classes enjoy this commandAction, itemStateChanged, isn't it good? Although this requires you to be very clear about the structure of the program:)
Inner classes also consume memory space in some ways, because the compiler needs to generate special variables and methods to provide private information about internal class entries.
3 use existing classes as much as possible. For example, CLDC-based profile does not construct collection classes, so we can implement it with built-in Hashtable and Vector classes. This method can also be used when constructing MIDP programs. In the example MIDlet, a form word class is defined to generate the main table, which can be generated directly as follows:
Mainform = new form ("Mainform")
Mainform.addCommand (okCommand)
Mainform.setCommandListener (listener)
There is no right or wrong here, because it is easy to understand.
4. Destroy the inheritance relationship of the program. You may put related code into one or more abstract classes, which is recommended in OOD to improve code reuse between programs. Although destroying the inheritance relationship is contrary to what you have learned, a simplified inheritance relationship makes more sense. In particular, when your abstract class-- possibly from another project-- is inherited only once, the result of breaking the inheritance relationship is self-evident. For example, the example MIDlet inherits the BasicMIDlet class, but the two are merged into one class.
5. Minimize the naming length of packages, classes, methods, and member variables you create. This sounds boring and stupid. However, a short naming of a class file with a lot of symbolic information will make you simplify the class file, which may not sound so important, but when several classes are derived, you will see its effect. Packages already have a mature set of simplified naming methods that try to avoid full package naming-without conflicts with other classes.
In addition, the work of short naming does not always need to be done manually, you can use tools to help you do it.
The obfuscator is a good choice. Its main purpose is to optimize naming (a concealment and reduction of your original naming style). The biggest effect of this process is to shrink the size of the application. This is mainly due to its renaming of the readability of methods and data variables (in compiled code).
Remember to pre-audit (preverification) before using the obfuscator, otherwise the obfuscator will invalidate the pre-audited data in the class file.
6, the initialization mode of the array. The initialization of an array is declared as follows:
Int arr [] = {0,1,2,3}
The actual compiled code is as follows:
Arr [0] = 0
Arr [1] = 1
Arr [2] = 2
Arr [3] = 3
This process can be seen by decompiling binaries into class files using the javap tool included with Java 2 SDK (using the-c option). You may be surprised by what you see, especially if you want to see rows of binary constants. There are two ways to prevent you from seeing the decompiled code, (1) encode the data as a string, decode it at run time, or (2) save the data as a binary file and package it with the program, and access it at run time using the getResourceAsStream method of the class loader.
The above is just a guideline. For J2ME programs, not all the steps are mentioned here, but most of the methods can be applied to the current example. Examples of optimized MIDP are as follows:
Import javax.microedition.lcdui.*
Import javax.microedition.midlet.*
Public class ASO extends MIDlet
Implements 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 MIDletStateChangeException {
ExitMIDlet ()
}
Public void exitMIDlet () {
NotifyDestroyed ()
}
Public Display getDisplay () {return display;}
Protected void initMIDlet () {
MainForm = new Form ("MainForm")
MainForm.addCommand (exitCommand)
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 ()
}
}
}
Thank you for your reading, the above is the content of "how to optimize the size of J2ME programs". After the study of this article, I believe you have a deeper understanding of how to optimize the size of J2ME programs, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.