1.å¦ä½è®¡ç®java对象å ç¨çå
å
2.scala manifeståclassmanifestçåºå«
3.javaä¸ä¸¤ä¸ªå符串çå
åå°åç¸å
å¦ä½è®¡ç®java对象å ç¨çå å
Javaæä¸ä¸ªå¾å¥½çå°æ¹å°±æ¯javaçåå¾æ¶éæºå¶ï¼è¿ä¸ªæºå¶éæäºjvmçï¼å¯¹ç¨åºåæ¥è¯´æ¯éèä¸ä¸éæçãè¿ç§æ åµä¸ï¼å¦ä½å¾å°æ个对象æ¶èçå åå¢ï¼
ãã
ããããæ¾ç»çå°è¿æ人ç¨ä»¥ä¸æ¹æ³æ¥è®¡ç®ï¼å¨çæ该objectçååé½è°ç¨java.lang.Runtime.freeMemory()æ¹æ³ï¼ç¶åç两è ä¹å·®å³ä¸ºè¯¥objectæ¶èçå åéã
ãã
ããããè¿ç§æ¹æ³ç代ç æ¯ï¼
ãã
ããlong totalMem = java.lang.Runtime.freeMemory();
ããObject myBigObject = null;
ããSystem.out.println("You just got rid of " + totalMem
ãã - java.lang.Runtime.freeMemory());
ãã
ãã
ãã
ããããè¿ç§æ³æ³æ¯å¯¹çï¼ä½æ¯å®é ä¸ï¼jvmçfreememoryå¾å¾ä¸è½æ£ç¡®ååºå®é çfree
memoryãæ¯å¦å¨jvmè¦è¿è¡åå¾æ¶éçæ¶åï¼free
memoryå°±ä¼ç¼©å°ãèå¦æå³å®åå¾æ¶éçæ¶é´åçå¨è¯¥objectçæä¹åï¼èå¨ç¬¬äºæ¬¡è°ç¨java.lang.Runtime.freeMemory()ä¹åï¼é£ä¹å°±ä¼é误å°å¢å 该objectæ¶èçå åéã
ãã
ããããå¨javaä¸å®¶By
Tony Sintesçæç« "Discover how much memory an object consumes "
éé¢æå°äºåºè¯¥ç¨Runtime.getRuntime().totalMemory();并ä¸è®¡ç®ä¸¤æ¬¡ä¹å·®æ¥å¾å°æ¶èçå åéã
ãã
ããããBy Tony Sintesçæºä»£ç ï¼
ãã
ããpublic class Memory {
ãã private final static int _SIZE = ;
ãã public static void main( String [] args )
ãã throws Exception {
ãã Object[] array = new Object[_SIZE];
ãã Runtime.getRuntime().gc();
ãã long start = Runtime.getRuntime().totalMemory();
ãã for (int i = 0; i < _SIZE; i++) {
ãã array[i] = new Object();
ãã }
ãã Runtime.getRuntime().gc();
ãã long end = Runtime.getRuntime().totalMemory();
ãã long difference = ( start - end ) / _SIZE;
ãã System.out.println( difference + " bytes used
ãã per object on average" );
ãã }
ãã}
ãã
ãã
ãã
ããããå®é ä¸ï¼è¿ç§æ¹æ³åºæ¬ä¸æ£ç¡®äºï¼ä½æ¯By Tony Sintesç忽äºä¸ç¹ï¼å°±æ¯ä» ä» Runtime.getRuntime().gc();并ä¸è½çæ£å®æåå¾æ¶éï¼ä¹å°±æ¯è¯´å®é ä¸jvmçå åæ¤æ¶å¹¶ä¸æ¯ç¨³å®çã
ãã
ããããæ以ï¼åªæå½å åä¸ååç大çåå¨ï¼æè 说已ç»ç¨³å®ï¼æ们æå¯è½è¯´åå¾æ¶éå·²ç»å®æã
ãã
ããããå¦ä½æè½çæ£ç¡®ä¿åºæ¬å®æäºjvmçåå¾æ¶éå¢ï¼å®ç°è¿ä¸ªåè½ç代ç å¦ä¸ï¼
ãã
ããprivate static final Runtime s_runtime =
ãã Runtime.getRuntime ();
ããprivate static long usedMemory ()
ãã {
ãã return s_runtime.totalMemory () -
ãã s_runtime.freeMemory ();
ãã }
ããprivate static void runGC () throws Exception
ãã {
ããlong usedMem1 = usedMemory (),托管赚钱 源码 usedMem2 = Long.MAX_value;
ããfor (int i = 0; (usedMem1 < usedMem2) && (i < ); ++ i)
ãã {
ãã s_runtime.runFinalization ();
ãã s_runtime.gc ();
ãã Thread.currentThread ().yield ();
ãã usedMem2 = usedMem1;
ãã usedMem1 = usedMemory ();
ãã }
ãã }
scala manifeståclassmanifestçåºå«
Manifestæ¯scala2.8å¼å ¥çä¸ä¸ªç¹è´¨ï¼ç¨äºç¼è¯å¨å¨è¿è¡æ¶ä¹è½è·åæ³åç±»åçä¿¡æ¯ãå¨JVMä¸ï¼æ³ååæ°ç±»åTå¨è¿è¡æ¶æ¯è¢«âæ¦æâæçï¼ç¼è¯å¨æTå½ä½Objectæ¥å¯¹å¾ ï¼æ以Tçå ·ä½ä¿¡æ¯æ¯æ æ³å¾å°çï¼ä¸ºäºä½¿å¾å¨è¿è¡æ¶å¾å°Tçä¿¡æ¯ï¼scalaéè¦é¢å¤éè¿Manifestæ¥åå¨Tçä¿¡æ¯ï¼å¹¶ä½ä¸ºåæ°ç¨å¨æ¹æ³çè¿è¡æ¶ä¸ä¸æã
def test[T] (x:T, m:Manifest[T]) { ... }
æäºManifest[T]è¿ä¸ªè®°å½Tç±»åä¿¡æ¯çåæ°mï¼å¨è¿è¡æ¶å°±å¯ä»¥æ ¹æ®mæ¥æ´åç¡®çå¤æTäºãä½å¦ææ¯ä¸ªæ¹æ³é½è¿ä¹åï¼è®©æ¹æ³çè°ç¨è è¦é¢å¤ä¼ å ¥måæ°ï¼é常ä¸å好ï¼ä¸å¯¹æ¹æ³ç设计æ¯ä¸é伤ç¤ã好å¨scalaä¸æéå¼è½¬æ¢ãéå¼åæ°çåè½ï¼å¨è¿ä¸ªå°æ¹å¯ä»¥ç¨éå¼åæ°æ¥åè½»è°ç¨è ç麻ç¦ã
è·åclass manifestsç两ç§åºæ¬æ¹å¼:
1 def classOf[T <: Any](implicit m: scala.reflect.Manifest[T]): Class[T] = m.erasure.asInstanceOf[Class[Tï¼½
éè¿implicit m: scala.reflect.Manifest[T]声æä¸ä¸ªéå¼åæ°ï¼è¿æ ·scalaç¼è¯å¨è½å¨ç¼è¯æ¶æä¾Tçç±»åä¿¡æ¯äº
2 def classOf[T <: Any : Manifest] : Class[T] = manifest[T].erasure.asInstanceOf[Class[Tï¼½
å ¶ä¸ T <: Any : Manifestï¼æåæ两é¨åæ¥ç
T <: Any
T æ¯Anyçåç±»åï¼å³å¯ä»¥æ¯ä»»æåºæ¬ç±»åscala.AnyVal åå¼ç¨ç±»å scala.AnyRef)
T : Manifest ç¸å½äºå¯¹classOf æ¹æ³currying
éå¼å¢å åæ°å表å¦ä¸ï¼(implicit evidence$1: Manifest[T])ï¼
éè¿manifest[T] æ¹æ³å³å¯è·åManifestå®ä¾
å¯è§å½¢å¼1 åå½¢å¼2å®è´¨æ¯ä¸æ ·çã
åºç¨ï¼
1æ常è§çæ¯è·åç±»ååæ°çClassï¼å½¢å¦someMethod[Type]
å¦akkaä¸æºç ï¼ def actorOf[T <: Actor : Manifest]: ActorRef = actorOf(manifest[T].erasure.asInstanceOf[Class[_ <: Actorï¼½)
class Worker extends Actor {
def receive = {
case Work(start, nrOfElements) =>
self reply Result(calculatePiFor(start, nrOfElements)) // perform the work
}
}
å°±å¯ä»¥å¦æ¤ä½¿ç¨äºï¼ val workerActorRef = actorOf[Worker]
2 ç¼ç¨æ¹å¼å建èåæ°ç»
def evenElems[T: ClassManifest](xs: Vector[T]): Array[T] = {
val arr = new Array[T]((xs.length + 1) / 2)
for (i <- 0 until xs.length by 2)
arr(i / 2) = xs(i)
arr
}
scala> evenElems(Vector("a","b","c"))
res: Array[java.lang.String] = Array(a, c)
javaä¸ä¸¤ä¸ªå符串çå åå°åç¸å
String s1 = new String("I am a student"); è¿é ä½ å£°æäºä¸ä¸ªå¼ç¨ s1\x0d\æåçæ¯ new String("I am a student"); è¿ä¸ªå符串\x0d\String s4 = s1; è¿é ä½ å声æä¸ä¸ªå¼ç¨ æås1çå¼ç¨ ä¹å°±æ¯new String("I am a student"); \x0d\if(s1 == s4) { \x0d\System.out.println("è¿ä¸¤ä¸ªå符串çå åä½ç½®ç¸å");\x0d\}\x0d\ä¸é¢è¿æ¯ç¸çç\x0d\ä½æ¯ s4 = s4.replace('a', 'A');\x0d\s4.replace('a', 'A'); çæäºå¦ä¸ä¸ª å符串 ä½ è¦ç¥é String æ¯finalç±»åç æ以\x0d\è¿ä¸ªæ¶å å³åæ¥å·²ç»å¼è¾äºå å空é´ç new String("I am a student"); æ¯ä¸å¯è½æ¹åå 容äºç\x0d\è¿ä¸ªæ¶å s4.replace('a', 'A'); å°±å¦å¼è¾äºä¸ä¸ªå å空é´\x0d\ èè¿ä¸ªæ¶å ä½ çS4æås4.replace('a', 'A'); èåæ¥çs1è¿æ¯æå new String("I am a student"); \x0d\ä»ä»¬è¯å®ä¸ä¸æ ·çå¢