最近使用JasperReport报表,JasperReport配合iReport用起来还算简单而且JasperReport的功能还是很强大的,不过由于JasperReport相应的文档比较少,遇到问题也比较多,现在就讲讲遇到的比较郁闷的一些问题.

1.生成PDF是中文显示不正确:这个由于JasperReport用itext的来生成PDF,而itext生成中文还需要itextasia的包,所以需要
itext.jar和itextasia.jar都在classpath中,而还需要讲文本的字体设为STsong_light并且PDF encoding设为:UniGB-UCS2-H (Chinese Simplified)或UniGB-UCS2-v (Chinese Simplified)选中PDF Embedded这样一般就没问题了

2.在Linux下JasperReport抛出Error initializing graphic environment的异常,经过分析得知,该错误是由于Jasperreport程序在转换为PDF时,采用了AWT方式,而AWT会调用操作系统本地窗口资源图.找到问题所在,就可以着手解决,解决的方法也很简单,就是让JVM启动时不检测图形界面,由于我们是用的是Tomcat,所以在 /tomcat/bin/catalina.sh 中添加以下的启动参数,即:
CATALINA_OPTS=’-Djava.awt.headless=true’

如果使用的不是Tomcat系统,可在启动JVM的地方添加:
JAVA_OPTS=’-Djava.awt.headless=true’
然后重新启动Tomcat,问题解决。

<br />注意:-Djava.awt.headless=true这个参数是在jdk1.4.1以后才引入的,如果系统使用的JDK是1.4以下的版本.<br />

对于 Java 1.4 以前,下载 xvfb 或者其他的虚拟 x-window 软件,再在 Java 运行环境里的 DISPLAY 作相应改变。

3.JasperReport的一些优化:
JasperRopert是先compile 再 fill 后 export 三步组成,这里compile是可以事先做好的,先将jrxml编译成jasper文件,当fill时直接fill这个jasper文件(可以读成InputStream,也可以直接给明路径)就可以了,这样在运行时就少了compile一步,速度有所提升,但缺点时每当改了jrxml都要自己先compile好才行哦.

This document follows the basic outline of the Java Programming Conventions
Guide, a copy of which may be found at http://geosoft.no/javastyle.html.



# Conventions



## General

Any violation to this guide is allowed if it enhances readability.

Guidelines in this document are informed by discussions carried out among the
Dojo core developers. The most weight has been given to considerations that
impact external developer interaction with Dojo code and APIs. Rules such as
whitespace placement are of a much lower order importance for Dojo developers,
but should be followed in the main in order to improve developer coordination.




## Quick Reference

Table of core API naming constructs:



































ConstructConventionComment
packagelowernever multiple words
classUpperLower 
public methodlowerUpperwhether class or instance method. lower_case() is acceptable only if the particular function is mimicing another API.
public varlowerUpper 
constantUpperLower or UPPER_LOWER 

Table of constructs that are not visible in the API, and therefore are optional
and carry less weight of enforcement.































ConstructConventionComment
private method_lowerUpper 
private var_lowerUpper 
method args_lowerUpper, lowerUpper 
local vars_lowerUpper, lowerUpper 




## Naming Conventions

1.

Names representing packages SHOULD be in all lower case.

2.

Names representing types (classes) MUST be nouns and written in
UpperLower case:

Account, EventHandler


3.

Constants SHOULD be placed within a single object created as a holder
for constants, emulating an Enum; the enum SHOULD be named
appropriately, and members SHOULD be named using either UpperLower or
UPPER_LOWER case:

var NodeTypes = {
Element : 1,
DOCUMENT: 2
}


4.

Abbreviations and acronyms SHOULD NOT be uppercase when used as a name:

getInnerHtml(), getXml(), XmlDocument


5.

Names representing methods SHOULD be verbs or verb phrases:

obj.getSomeValue()


6.

Public class variables MUST be written using upperLower case.

7.

Private class variables MAY be written using _upperLower (with
preceding underscore):

var MyClass = function(){
var buffer;
this.doSomething = function(){
};
}


8.

Variables that are intended to be private, but cannot be based on the
semantics of Javascript, SHOULD prepended with a "" (underscore) char:

this._somePrivateVariable = statement ;

NB Note that the above variable also follows the convention for a
private variable.

9.

Generic variables SHOULD have the same name as their type:

setTopic(topic) //  where topic isTypeOf Topic


10.

All names SHOULD be written in English.

11.

Variables with a large scope SHOULD have globally unambiguious names,
ambiguity MAY be distinguished by package memebership. Variables with
small or private scope MAY be more terse still.

12.

The name of the return object is implicit, and SHOULD be avoided in a
method name:

getHandler(); //    NOT getEventHandler()


1.

Public names SHOULD be as clear as necessary and SHOULD avoid unclear
shortenings and contractions:

MouseEventHandler, NOT MseEvtHdlr.

Note that, again, any context that can be determined by package
membership SHOULD be used when determining if a variable name is clear.
For example, a class that represents a mouse event handler:

dojo.events.mouse.Handler, NOT dojo.events.mouse.MouseEventHandler


2.

Classes/constructors MAY be named based on their inheritance
pattern, with the base class to the right of the name:

EventHandler
UIEventHandler
MouseEventHandler

NB The base class CAN be dropped from a name if it is obviously
implicit in the name:

MouseEventHandler as opposed to MouseUIEventHandler.




## Specific Naming conventions

1.

The terms get/set SHOULD NOT used where a field is accessed, unless the
variable being accessed is lexically private.

2.

"is" prefix SHOULD be used for boolean variables and methods
NB. Alternatives include "has", "can" and "should"

3.

The term "compute" CAN be used in methods where something is computed.

4.

The term "find" CAN be used in methods where something is looked up.

5.

The terms "initialize" or "init" CAN be used where an object or a
concept is established.

6.

UI Control variables SHOULD be suffixed by the control type.
ex. leftComboBox, topScrollPane

7.

Plural form MUST be used to name collections.

8.

"num" prefix or "count" postfix SHOULD be used for variables
representing a number of objects.

9.

Iterator variables SHOULD be called "i", "j", "k", etc.

10.

Compliment names MUST be used for compliment entities.
ex. get/set, add/remove, create/destroy, start/stop, insert/delete,
begin/end, etc.

11.

Abbreviations in names SHOULD be avoided.

12.

Negated boolean variable names MUST be avoided:

isNotError, isNotFound are unacceptable.


13.

Exception classes SHOULD be suffixed with "Exception" or "Error" .. FIXME (trt) not sure about this?

14.

Methods returning an object MAY be named after what they return, and
methods returning void after what they do.




## Files

1.

Class or object-per-file guidelines are not yet determined.

2.

Tabs (set to 4 spaces) SHOULD be used for indentation.

3.

If your editor supports "file tags", please append the appropriate tag
at the end of the file enable others to effortlessly obey the correct
indentation guidelines for that file:

// vim:ts=4:noet:tw=0:


1.

The incompleteness of split line MUST be made obvious:

var someExpression = Expression1
+ Expression2
+ Expression3 ;

var o = someObject.get(
Expression1,
Expression2,
Expression3
);

Note the indentation for expression continuation is indented relative
to the variable name, while indentation for parameters is relative to
the method being called.

Note also the position of the parenthesis in the method call;
positioning SHOULD be similar to the use of block notation.




## Variables

1. Variables SHOULD be initialized where they are declared and they SHOULD
be declared in the smallest scope possible. A null initialization is
acceptable.
2. Variables MUST never have a dual meaning.
3. Related variables of the same type CAN be declared in a common
statement; unrelated variables SHOULD NOT be declared in the same
statement.
4. Variables SHOULD be kept alive for as short a time as possible.
5. Loops / iterative declarations

1. Only loop control statements MUST be included in the "for()"
construction.
2. Loop variables SHOULD be initialized immediately before the loop;
loop variables in a "for" statement MAY be initialized in the "for"
loop construction.
3. The use of "do…while" loops are acceptable (unlike in Java)
4. The use of "break" and "continue" is not discouraged (unlike in Java)

6. Conditionals

1. Complex conditional expressions SHOULD be avoided; use temporary
boolean variables instead.
2. The nominal case SHOULD be put in the "if" part and the exception
in the "else" part of an "if" statement.
3. Executable statements in conditionals MUST be avoided.

7. Miscellaneous

1. The use of magic numbers in the code SHOULD be avoided; they SHOULD
be declared using named "constants" instead.
2. Floating point constants SHOULD ALWAYS be written with decimal
point and at least one decimal.
3. Floating point constants SHOULD ALWAYS be written with a digit
before the decimal point.



## Layout

1.

Block statements.
1. Block layout SHOULD BE as illustrated below:

while(!isDone){
doSomething();
isDone = moreToDo();
}


1.

If statements SHOULD have the following form:

if(someCondition){
statements;
}else if(someOtherCondition){
statements;
}else{
statements;
}


2.

for statements SHOULD have the following form:

for(initialization; condition; update){
statements;
}



1.

while statement SHOULD follow the form in example VI.A.1.

2.

a do…while statement SHOULD have the following form:

do{
statements;
}while (condition);


3.

a switch statement SHOULD have the following form:

switch (condition){
case ABC:
statements;
// fallthrough
case DEF:
statements;
break;
default :
statements;
break;
}


4.

A try…catch…finally statement SHOULD have the following form:

try{
statements;
}catch (ex){
statements;
}finally{
statements;
}


5.

Single statement if-else, while or for MUST NOT be written without
brackets, but CAN be written on the same line:

if(condition){ statement; }
while(condition){ statement; }
for(intialization; condition; update){ statement; }


2.

Whitespace

1. Conventional operators MAY be surrounded by a space (including
ternary operators).
2. Reserved words SHOULD be followed by a space.
3. Commas SHOULD be followed by a space.
4. Colons MAY be surrounded by a space.
5. Semi-colons in for statements SHOULD be followed by a space.
6. Semi-colons SHOULD NOT be preceded by a space.
7. Functions/method calls SHOULD NOT be followed by a space.
ex. doSomething(someParameter); // NOT doSomething (someParameter)
8. Logical units within a block SHOULD be separated by one blank line.
9. Statements MAY be aligned wherever this enhances readability.

3.

Comments

1. Tricky code SHOULD not be commented, but rewritten.
2. All comments SHOULD be written in English.
3. Comments SHOULD be indented relative to their position in the code,
preceding or to the right of the code in question.
4. The declaration of collection variables SHOULD be followed by a
comment stating the common type of the elements in the collection.
5. Comments SHOULD be included to explain BLOCKS of code, to explain
the point of the following block.
6. Comments SHOULD NOT be included for every single line of code.

内容:
http://dojotoolkit.org/docs/compressor_system.html

在Dojo的build过程最后一个可选步骤是compression(压缩).像其它JavaScript工具一样,Dojo也用工具减少代码量,可以被浏览器更快的下载.
当然,太多的压缩工具存在,但各方面因素决定Dojo的压缩工具是唯一的.
Example
为了运行以下的例子,推荐你安装jdk1.4.从Dojo官网下载压缩工具.
http://dojotoolkit.org/svn
如果你看了前文,D:\zkj\dojosource\buildscripts\lib下有这个包

然后编辑infile.js文件,内容如下:

function MyClass(){
this.foo = function(argument1, argument2){
var addedArgs = parseInt(argument1)+parseInt(argument2);
return addedArgs;
}

var anonymousInnerFunction = function(){
// do stuff here!
}
}

function MyFunc(){
// this is a top-level function
}

// 你可以输入一些空格

我们在命令行输入以下命令得到 outfile.js文件

java -jar custom_rhino.jar -c infile.js > outfile.js 2>&1
outfile.js的内容是以下代码.

function MyClass(){
this.foo=function(_1,_2){
var _3=parseInt(_1)+parseInt(_2);
return _3;
};
var _4=function(){
};
}
function MyFunc(){
}

你可以比较一下文件大小.
obelisk:/tmp/compress alex$ ls -lah

-rw-r–r– 1 alex users 321B Aug 12 09:21 infile.js
-rw-r–r– 1 alex users 140B Aug 12 09:21 outfile.js
321 bytes to 140 bytes, a 56% reduction. Not bad!

我在window操作系统,但看代码也知道压缩不少.

Riding a Rhino

这些工作是怎么完成的呢?为什么选择这个工具而不是其它呢?Dojo的compressor基础是Rhino. Rhino是来自the Mozilla project的javascript解释引擎(java实现).而其它一些压缩工具一般是自定义的规则表达式来完成压缩的.
相比那些regular-expression为基础的工具,通过Rhino对js代码的解析,Dojo compressor能得到更好的效果,体现在变量的上下文环境,变量名等.Dojo compressor不会改变代码的public API.

API Safety

There are many "obfuscators" available in addition to size reduction tools. Over the years, many people have attempted to "encrypt" or otherwise obfuscate JavaScript sent over the wire to browsers, and it never pans out. Why not? For starters, JavaScript (as implemented in browsers) is completely interpreted. This means that any further compilation beyond source transformations will not work everywhere, and the tool provides a "decryption" tool along with the "encrypted" or obfuscated source, the unencrypted version will be available at runtime for anyone with a debugger to see. For those tools that just transform source code by mangling variable names, it’s even easier to revert their changes. Therefore, obfuscation and encryption aren’t useful goals. Size reduction, on the other hand, is a useful goal.
But not if your size-reduction tool breaks things. There are, of course, many increments available for the "compression" process. Potential choices available to a tool author include:
· removing comments
· collapsing line-beginning whitespace
· removing line-ending whitespace
· collapsing multiple blank lines
· removing all new-line characters
· removing whitespace around operators
· removing whitespace near/around curly braces
· replacing symbols with shorter names (this is how most "obfuscation" is done)
And the list goes on and gets ever-more esoteric as one tries to squeeze every last K out of a JavaScript file. But at some point, you can go too far. The Dojo compressor attempts to strike a balance between debuggability (not replacing all symbols and not removing all newlines) and size reduction.
Getting The Source
Rhino的源代码可以从Mozilla CVS服务器得到:
http://www.mozilla.org/cvs
具体路径在:
/cvsroot/mozilla/js/rhino
Our patches should apply cleanly against Rhino HEAD and are available in unified diff format from:
http://dojotoolkit.org/svn
Unlike the rest of Dojo, the Dojo Foundation does not control the copyright of the original work, and we therefore cannot license this code under there AFL. It is made available under the tri-licensing terms of the Mozilla project.
The Future
The Dojo compression tool is by no means the last word in file-size or on-the-wire reduction. Gzipping content on the wire is the next obvious improvement for those deploying applications.
The Dojo package system and JSAN allow developers to include just those packages from a library that they require, and future work on real JS linkers will further strip down capable libraries like Dojo to only absolutely what is needed by application authors.
The Dojo project intends to continue to produce the best Open Source tools for JS and webapp developers, and we will make these transparently available in the Dojo build system, as we do today with the compression and package systems.
About The Author
Alex Russell is the project lead for Dojo and can be reached at . His blog is at: http://alex.dojotoolkit.or

Purpose

利用Dojo的packaging

system,只要引入一个简单的可定制的script(dojo.js)文件,通过简单的代码就可以从Dojo的source
tree中找到并获得(下载)你所需的packages,当然这些packages是你应用程序需要的。这些功能实现不需要你了解Dojo源码结构,也不
需要为你下载的script file加script标签。

对于Dojo
0.3以上版本,dojo.hostenv.conditionalLoadModule()方法已经被dojo.kwCompoundRequire
()方法替换。dojo.hostenv.moduleLoaded()也已经被dojo.provide()替换。如果你在Dojo0.3+中用老的方
法,请看compatibility package.

Example package file format(包文件中的格式)

// dojo/src/webui/widgets/package.js中的内容


dojo.kwCompoundRequire({
common: ["dojo.webui.widgets.parse"] // a generic dependency
});

dojo.provide("dojo.webui.widgets.*");

Definitions

kwCompoundRequire
这个方法调用会根据dojo的执行环境,选择性的下载文件,如src\webui\widgets\parse.js
provide
大致意思是告诉用户,dojo提供了dojo.webui.widgets.包,你可以dojo.require("dojo.webui.widgets.");来访问,这样会把kwCompoundRequire的参数中都下载的。
A Longer Example

这段代码的位置在 dojo/src/foo/package.js:


dojo.kwCompoundRequire({
common: ["dojo.foo.Foo", "dojo.foo.Bar"],
browser: ["dojo.foo.Baz"]
});

dojo.provide("dojo.foo.*");

这一大堆代码告诉我们,当我们在浏览器环境中写dojo.require("dojo.foo.*")时,Foo.js, Bar.js, 和 Baz.js将被包括,当在command line(命令行运行时),只有Foo.js and Bar.js被包括。

Including Dojo from source files

如果你要用dojo,你只需要在你的html代码中加入

<script type="text/javascript" src="/dojo/dojo.js"></script>

如果你要加载Dojo的某些包,你只需要简单的用dojo.require就可以实现。例如你要加载Dojo的事件模块,你只需要dojo.require("dojo.event.Event");

自己打包dojo.js文件(原文写的缺少步骤,自己组织了下)

1、从http://svn.dojootoolkit.or… 下载源码。偶用小绿毛龟TortoiseSVN
2、安装jdk,ant包。
2、把foo.profile.js放到源码的buildscripts\profiles文件夹下( D:\zkj\dojosource\buildscripts\profiles)

foo.profile.js 文件名称


var dependencies = [
"dojo.io.",
"dojo.event.
",
"dojo.xml.",
"dojo.graphics.
",
"dojo.io.BrowserIO",
"dojo.webui.",
"dojo.webui.widgets.foo.
",
];

load("getDependencyList.js");
3、在D:\zkj\dojosource\buildscripts文件夹下执行命令 ant -Dprofile=foo clean release
4、在D:\zkj\dojosource\release文件夹下你就看到你自己打包的dojo了。

自己打包有什么好处:
1、可以根据自己的定义(.profile.js),确定我们预先把哪些模块直接打包到dojo.js文件中。
2、对于dojo通过xmlhttp下载js代码效率上我是有写担心,如果带项目运行阶段,我们完全可以把用到的所有模块打包到dojo.js中,减少的动态加载。

翻译至dojo的FAQ只是节选了一些比较容易碰到的问题,还有一些自己遇到的问题,希望对大家有帮助:

1.为啥IE会说有个语法错误?

很有可能你是在列表后面多了个逗号如下所示:

var request = dojo.io.bind({
url: this.actionURL,
method: ‘get’,
content: {
id: this.rowId,
field: this.dbField,
value: this.checkbox.checked
}, <—- 这就是你多的逗号
});

或Or:

var foo = [ 1 , 2, 3, ];

或:

var obj = {
apple: "delicious",
brussel sprouts: "yucky",
};

这个问题很普遍,而且在Firefox下是可以解析的,而在IE下会使整段script代码失效,这样调试起来也很麻烦,着实让人郁闷.

2.为啥页面载入的这么慢?

一些用dojo.require()载入的资源是通过同步XHR来从服务器来读取的?这在开发的时候很方便,但在部署的时候就会出现网页载入很慢的问题.实际上可以用打包的方法来将一些经

常用的js打包到一个dojo.js中,然后在对这个dojo.js压缩一下,这样就会快很多(关于打包和压缩将会在以后具体说明).

3.dojo.addOnLoad 没有在页面载入后执行

为了使dojo.addOnLoad正确的执行,你需要将代码写在一个init函数中,然后再用dojo.addOnLoad执行,例下:

这个是不对的:dojo.addOnLoad(alert("this page is not yet loaded");
你应该这么写:
function init() {
alert("this page is fully loaded!");
}
dojo.addOnLoad(init);

alert方法应该写在init中,再用dojo.addOnLoad载入,才能被正确的执行.

dojo.addOnLoad在firefox中执行也有问题,有时在Document未解析完前就执行了,这样有些DomNode会取不到而出现问题,而在IE却是正常的…..看来这个问题也有待解决!
所以最好还是在一些DomNode的onload事情中执行函数才能在firefox和IE中都正常运行.

最近的做的这个项目,用dojo的Iframe来和后台进行交互,可是郁闷的事情来了,在Firefox下是好的,可是到ie后,当将数据传到后台后原本是中文的全变成乱码了,这下郁闷了,Firefox下是好的呀,用HTTPDebug看了一下Request发现应该是POST操作的,在ie全变成GET了..而且Content-Type也为null.查了一下代码,明明已经在dojo.io.bind中注明method:"post"了,这下我就以为是ie坏了,可是到别人的电脑一试也是一样,看来还是代码的问题,抱着试试看的想法,我在那些需要提交Form上加上了method为post的属性,这一加ie就可以了,这个困扰了我一晚上的问题,就家里一条属性就解决了!…
看来dojo在ie下用io来定义method是没用的,还得在form中加上method属性;
Firefox和ie在解析javascript上也是有一定的区别的.在Firefox上运行成功的程序在ie就会通不过,可能原因就是小小的var,有时ie会认为没有var的变量是无法使用的,而Firefox却没什么问题.
而CSS在Firefox中和IE中的差的更多….不知道什么时候能统一哟!

<![CDATA[

最近对程序占用内存方面做了一些优化,取得了不错的效果,总结了一些经验
简要说一下,相信会对大家写出优质的程序有所帮助
下面的论述针对32位系统,对64位系统不适用,后叙

经常你写了一个程序,一测试,功能没问题,一看内存占用也不多,就不去考虑其它的东西
了。但可能程序使用了一个什么数据结构,会当数据规模变大时,内存占用激增。

基本&关键的问题是,Java里各种东东占多少内存?????????

对于primitive类型,有8个
byte short int long float double char boolean 它们的长度分别是
1 2 4 8 4 8 2 1
这个不罗嗦了,举例来说
long[] data=new long[1000];
占用内存 81000 bytes
此外,data本身是一个Object,也占用内存若干,后叙,当然它针对 8
1000来说,忽略不

再说Object的占用,在说这个之前,先说说引用,一惯的说法是
Java里没有指针了,只有引用,引用是安全的

这个说法没错,但是从机理上来说,引用就是指针,只是jvm对指针的使用检查和限制很多
,这个引用/指针变得很安全

直接来结论:一个引用占4byte ,在32位系统上

Object obj=null; //4byte
Object[] objs=new Object[1000]; //至少4*1000byte

你看我定义了一个 obj,还是null,就占4byte
定义了一个 objs,1000个元素,但都是null啊,就都每个占4byte
是的!!!!!
虽然obj==null,但它已经是 一个引用,或者说一个指针了
指针也要占地方啊!!!!啊!!!!啊!!!!

接下来,直接给另一个结论: Object占8byte,注意,纯Object

Object obj=new Object(); //多少????

8byte?? 错!! 12byte,忘了还有一个引用,8byte是Object的内容
记住 Object obj=new Object(); 占12byte

Object[] objs=new Object[1000];
for(int i=0;i<1000;i++) {
objs[i]=new Object();
}

至少占用 12*1000 bytes

推论: Object占12bytes,似乎和上面的结论矛盾??!!
没有!! 不管Object,没有被垃圾回收之前,总得被别人引用吧?
总的有指针指它吧? 既然指,那个引用or指针就要占地方啊 4byte
加起来是12byte,反正一个Object至少 12bytes

还是直接给结论,推导的过程我就都包办了,咱不是脏活累活抢着干么!!
一个Integer占 16 bytes

这时您可能会有疑问,Integer=Object+int,就是:
public class Integer {
public int value;
}
Integer应该占 8+4=12 bytes啊
你说的有道理,但是jvm对所有的Object有限制!!
这个限制被我发现了,就是不管什么Object占的空间,要是8的倍数
12不是8的倍数,只能是16了!!!

推论:Byte也占16bytes!!!!!!!!!!!

问:
Byte[] bytes=new Byte[1000];
占用空间多少?
答: 约为(至少为) (16+4)*1000 bytes
好家伙!!!!!!!!

论题:数组空间占用怎么算?
我这里直接给结论了,推导这个花了更长的时间:
对于数组来说,数组这个Object有一个length属性,数组的元素相当于其成员
public class Array {
public int length;
//… 其它成员
}
对于数组,我们不是直接可以取length属性么,源于此

public byte[] bytes=new byte[1000];
System.out.println(bytes.length); // 看,有length属性
上面的bytes换算过来是:
public class Array {
public int length;
public byte byte0;
public byte byte1;

public byte byte999;
}
上面的bytes占用的内存是:
4+[8+4 + 11000] = 4+ [1012]=4+1016=1020
4是 bytes这个引用,8是Object基占的,4是length属性占的
1000是1000个成员占的,本来是 1012,但要求是8的倍数,变成 1016了
总共是 1020
再如:
byte[] bytes=new byte[4];
的内存占用是:
4+[8+4+4
1]=4+[16]=20;

byte[] bytes=new byte[3]; 也是 20

对于元素是Object的数组,Object也是当作其成员,(注意只有引用这个数组的空间,这个
可以推到普通Class上)

Byte[] bytes=new Byte[1000];
这个 bytes的定义相当于:
public class Array {
public int length;
public Byte byte0;
…..
public Byte byte999;
}
占用空间是:
4+[8+4+41000]+161000= 4+ 4016 + 16000 = 你自己算吧

推论:千万不要用 Byte[] 有20倍的差距!!!!!!!

你可能一下子没明白过来,没关系多琢磨一下,对于普通的class来说
,内容占用就是基加成员的占用,Object成员只记引用
public class Abc {
public int n;
public byte b;
public Object obj;
}
它的内容占用是: [8+4+1+4]=24
所以 Abc one=new Abc()的占用是 4+24=28
提醒:对于 Abc的成员 obj没有计,如果要计入的话,循环这个过程就可以了。(琢磨一下

举例:

public class Abc {
public byte b;
public Object obj=null;
}

public class Def {
public int n;
public byte b;
public Abc obj=new Abc();
}
问:
Def one=new Def(); //占多少?
答:
4+[8+4+1+4]+[8+1+4]=4+24+16=44

public class Abc {
public byte b;
public Object obj=null;
}

public class Def {
public int n;
public byte b;
public Abc[] objs=new Abc[100];
{
for(int i=0;i<10;i++) {
objs[i]=new Abc();
}
}
}
问:
Def one=new Def(); //占多少?
答:
kao,一下我也算不出来,不过我写了程序,可以算出来,你给它一个Object,它就能递归
的算出总共占了多少内存,这个程序不复杂,你也可以写出来。我等机会合适了再放出。

单独说一下String,String的结构是:
public class String {
private final char value[];
private final int offset;
private final int count;
private int hash; // Default to 0
}
所以,不考虑那个char[]的占用,一个String最少占用 [8+4+4+4+4]=24bytes
加上引用,共28bytes
所以
String s="";
占用28bytes!!!!! 尽管它的长度为0
如果精确的算,加上引用一个String的占用是
4+24+[8+4+2*length]
String s=""; 的占用是 28+16= 44
String s="ab" 的占用是 28+16= 44
String s="abc" 的占用是 28+24 = 52

要说的是,String是常用的类,这么看,String耗内存很多,所以jvm有优化,同样的内容
尽量重用,所以除了28是必须的外,那个char[] 很可能一样
比方说
String[] s=new String[1000];
for(int i=0;i<1000;i++) {
s[i]=new String("abcdefasdjflksadjflkasdfj");
}
的占用的数量级是 28*1000,那 1000个字符串本身基本上不占内存,只有一份!!!!!

反正String 至少是 28,最多也可能是28!!!!!!!!

比较占内存的数据结构,这个很重要:
基本上就是 primitive的包装

实例:
我以前用一个
Hashtable<String,Integer>的结构,有100万个元素
改为String[]+int[]后,内存占用改观不少,速度也很快
100万的String[] 快排一下,也就2秒多,查找用2分,和hash也差不多少

完!

<

p>
说明:
1。 以上结论适用于32位系统,对于64位系统,有很多不同。反正结论是

今天用了一下DropdownDatePicker,发现这次的DropdownDatePicker比之0.3.1有了质的飞跃:
DropdownDatePicker在0.3.1的时候是以div的形式出现的.这着实很让人郁闷.因为在form提交的时候div是不被解析的,那怎么去取DropdownDatePicker值呢,就只有用隐藏的text域

加上event来赋值达到效果.实在是太费事了…这次在0.4.0中DropdownDatePicker是以input形式出现的,这实在太"激动人心"了,form直接提交就可以了,而且比之0.3.1的

DropdownDatePicker多了很多的事件和方法,enable,disable,getValue等等,配合0.4.0新出的dojo.date函数使DropdownDatePicker的应用更加得心应手.而且比之0.3.1单一的wipe

效果,多了explode和fade效果更眩了.hoho

ComboBox这次支持用dataUrl远程获得json的数据了,看了一下源代码,它是用dojo.io.bind来获得的,所以在远程的数据中不能用var一个变量来赋值(这样复用就难了)…好像和原

来的数组也没啥区别,而且觉得比0.3.1的ComboBox更加难看了………不过比0.3.1多了很多的方法,事件和DropdownDatePicker差不多.不过ComboBox也有郁闷的地方.就是

ComboBox在html页面表示的时候是用两个隐藏text域来表现选中的数据的(一个前台的表现域显示的是具体数据,一个是后台的域放着的是主键form提交的时候传的就是这个值)所以

在给ComboBox赋值的时候要两个域都赋,函数是setValue()和setSelectedValue(),也可以用setAllValue()来一起赋,郁闷的就是当只知道其中一个值的时候是不能完美的将

ComboBox显示出来的…好像也没什么办法来获得json数据来判断…不知道以后的版本会不会有改进.

好,今天就说到这里…

Dojo 0.4.0已经推出了.相较上一个版本0.3.1 这次有了很多的改动和改进,终于推出了其API(实际上就是将部分对应的源代码给贴出来..对初学还是很有用的,不过还是有很多属性

和一些隐藏的方法没写出来,所以还是直接看源代码比较方便),经过了一段时间的试用,我也来总结一下经验(主要还是widget这部分)
1.这次0.4.0支持多语言了,终于看到DatePicker,validate中组件中出现中文了,真是大快人心.不过因为中文有zh_cn和zh之分,有时候会读不到相应的文件,不过自己copy一下文件

到相应的目录就没问题了.开始dojo要做大一统toolkit的路线已经一步步在展开了.
2.这次widget中多了个FilteringTable组件,比之以前简陋的SortableTable有了很多的扩展.而且在0.5.0中将取消SortableTable了,所以好好研究了

FilteringTable.FilteringTable相比SortableTable多了过滤功能,根据条件可以将数据进行过滤.还是就是FilteringTable,它将dojo.collections.Store作为它的后台数据存储容

器,如果要将json数据解析到FilteringTable,只要用FilteringTable.store.setData(data)就可以将数据直接展示到前台了,比之SortableTable的parserData和render方便了很多,

不过FilteringTable也有很多方面着实让我郁闷了一把:
(1)就是它不支持ctrl键了,如果开启了multiple,那么选一行就多了一行了..没单选了…没办法自己改了一下源代码…-..-!! 在onSelect函数中加入对e.ctrlKey的判断和

resetSelection();就可以了.
(2)当自己的数据导入FilteringTable居然不能排序,而且一选就变成全选,而且还不能取消.这一度使我想放弃FilteringTable,不过后来还是找到了解决方法,在dojo的mail list

也有人提到.就是在tbody的tr中需要有个value的属性就可以解决……如果是外部数据导入怎么办呢…不是那在初始化FilteringTable的时候不是有个valueField的属性么,以前

一直不知道这个是干吗用的,这个就是用来设 tbody中tr的value属性用的.将valueField指定为外部json的一个字段名(实际上就是为了得到一个唯一的id啦.),那么FilteringTable

就会自动将可以将json的这个字段名的值赋到tbody的tr中需要有个value的属性中,那么就可以选择和排序了.
3.其他的比如多了clock组件啊,什么的都是一些有趣的东西..下次慢慢在来介绍.