¡¡¡¡java ÓïÑÔÖÐÒ»¸öÏÔÖøµÄÌصã¾ÍÊÇÒýÈëÁËjava»ØÊÕ»úÖÆ£¬ÊÇc++³ÌÐòÔ±×îÍ·ÌÛµÄÄÚ´æ¹ÜÀíµÄÎÊÌâÓÈжø½â£¬ËüʹµÃjava³ÌÐòÔ±ÔÚ±àд³ÌÐòµÄʱºò²»ÔÚ¿¼ÂÇÄÚ´æ¹ÜÀí¡£ÓÉÓÚÓиöÀ¬»ø»ØÊÕ»úÖÆ£¬javaÖеĶî¶ÔÏó²»ÔÚÓС°×÷ÓÃÓò¡±µÄ¸ÅÄֻÓжÔÏóµÄÒýÓòÅÓС°×÷ÓÃÓò¡±¡£À¬»ø»ØÊÕ¿ÉÒÔÓÐЧµÄ·ÀÖ¹ÄÚ´æй¶£¬ÓÐЧµÄʹÓÿÕÏеÄÄڴ棬ÏÂÃæÊÇÓйØJavaÀ¬»ø»ØÊÕ»úÖÆÒÔ¼°ÄÚ´æй©ÎÊÌâÂÛÎÄ£¬»¶Ó²Î¿¼ÔĶÁ£¡
¡¡¡¡Ç°ÑÔ
¡¡¡¡ÔÚsegmentfaultÉÏ¿´µ½Ò»¸öÎÊÌ⣺javaÓÐÍêÉƵÄGC»úÖÆ£¬ÄÇôÔÚjavaÖÐÊÇ·ñ»á³öÏÖÄÚ´æй©µÄÎÊÌ⣬ÒÔ¼°ÄÜ·ñ¸ø³öÒ»¸öÄÚ´æй©µÄ°¸Àý¡£±¾ÎÊÌâÊÓͼ¸ø³ö´ËÎÊÌâµÄÍêÕû´ð°¸¡£
¡¡¡¡À¬»ø»ØÊÕ»úÖƼò½é
¡¡¡¡ÔÚ³ÌÐòÔËÐйý³ÌÖУ¬Ã¿´´½¨Ò»¸ö¶ÔÏ󶼻ᱻ·ÖÅäÒ»¶¨µÄÄÚ´æÓÃÒÔ´æ´¢¶ÔÏóÊý¾Ý¡£Èç¹ûÖ»ÊDz»Í£µÄ·ÖÅäÄڴ棬ÄÇô³ÌÐò³ÙÔçÃæÁÙÄÚ´æ²»×ãµÄÎÊÌâ¡£ËùÒÔÔÚÈκÎÓïÑÔÖУ¬¶¼»áÓÐÒ»¸öÄÚ´æ»ØÊÕ»úÖÆÀ´ÊͷŹýÆÚ¶ÔÏóµÄÄڴ棬ÒÔ±£Ö¤ÄÚ´æÄܹ»±»Öظ´ÀûÓá£
¡¡¡¡ÄÚ´æ»ØÊÕ»úÖÆ°´ÕÕʵÏÖ½ÇÉ«µÄ²»Í¬¿ÉÒÔ·ÖΪÁ½ÖÖ£¬Ò»ÖÖÊdzÌÐòÔ±ÊÖ¶¯ÊµÏÖÄÚ´æµÄÊÍ·Å£¨±ÈÈçCÓïÑÔ£©ÁíÒ»ÖÖÔòÊÇÓïÑÔÄÚ½¨µÄÄÚ´æ»ØÊÕ»úÖƱÈÈç±¾ÎĽ«Òª½éÉܵÄjavaÀ¬»ø»ØÊÕ»úÖÆ¡£
¡¡¡¡JavaµÄÀ¬»ø»ØÊÕ»úÖÆ
¡¡¡¡ÔÚ³ÌÐòµÄÔËÐÐʱ»·¾³ÖУ¬javaÐéÄâ»úÌṩÁËÒ»¸öϵͳ¼¶µÄÀ¬»ø»ØÊÕ£¨GC£¬Carbage Collection£©Ị̈߳¬Ëü¸ºÔð»ØÊÕʧȥÒýÓõĶÔÏóÕ¼ÓõÄÄÚ´æ¡£Àí½âGCµÄÇ°ÌáÊÇÀí½âһЩºÍÀ¬»ø»ØÊÕÏà¹ØµÄ¸ÅÄÏÂÎÄÒ»Ò»½éÉÜÕâЩ¸ÅÄî¡£
¡¡¡¡¶ÔÏóÔÚjvm¶ÑÇøµÄ״̬
¡¡¡¡Java¶ÔÏóµÄʵÀý´æ´¢ÔÚjvmµÄ¶ÑÇø£¬¶ÔÓÚGCÏß³ÌÀ´Ëµ£¬ÕâЩ¶ÔÏóÓÐÈýÖÖ״̬¡£
¡¡¡¡1. ¿É´¥¼°×´Ì¬£º³ÌÐòÖл¹ÓбäÁ¿ÒýÓã¬ÄÇô´Ë¶ÔÏóΪ¿É´¥¼°×´Ì¬¡£
¡¡¡¡2. ¿É¸´»î״̬£ºµ±³ÌÐòÖÐÒѾûÓбäÁ¿ÒýÓÃÕâ¸ö¶ÔÏó£¬ÄÇô´Ë¶ÔÏóÓÉ¿É´¥¼°×´Ì¬×ªÎª¿É¸´»î״̬¡£CGÏ߳̽«ÔÚÒ»¶¨µÄʱ¼ä×¼±¸µ÷Óô˶ÔÏóµÄfinalize·½·¨£¨finalize·½·¨¼Ì³Ð»òÖØд×ÓObject£©£¬finalize·½·¨ÄڵĴúÂëÓпÉÄܽ«¶ÔÏóתΪ¿É´¥¼°×´Ì¬£¬·ñÔò¶ÔÏóת»¯Îª²»¿É´¥¼°×´Ì¬¡£
¡¡¡¡3. ²»¿É´¥¼°×´Ì¬£ºÖ»Ó屶ÔÏó´¦ÓÚ²»¿É´¥¼°×´Ì¬Ê±£¬GCÏ̲߳ÅÄÜ»ØÊմ˶ÔÏóµÄÄÚ´æ¡£
¡¡¡¡GCΪÁËÄܹ»ÕýÈ·ÊͷŶÔÏ󣬱ØÐë¼à¿Øÿһ¸ö¶ÔÏóµÄÔËÐÐ״̬£¬°üÀ¨¶ÔÏóµÄÉêÇë¡¢ÒýÓᢱ»ÒýÓᢸ³ÖµµÈ£¬GC¶¼ÐèÒª½øÐмà¿Ø£¬ËùÒÔÎÞÂÛÒ»¸ö¶ÔÏó´¦ÓÚÉÏÎÄÖеÄÈκÎ״̬GC¶¼»áÖªµÀ¡£
¡¡¡¡ÉÏÎÄ˵µ½£¬GCÏ̻߳áÔÚÒ»¶¨µÄʱ¼äÖ´Ðпɸ´»î״̬¶ÔÏóµÄfinalize·½·¨£¬ÄÇôºÎʱִÐÐÄØ£¿ÓÉÓÚ²»Í¬µÄJVMʵÏÖÕß¿ÉÄÜʹÓò»Í¬µÄËã·¨¹ÜÀíGC£¬ËùÒÔÔÚÈκÎʱºò£¬¿ª·¢ÕßÎÞ·¨Ô¤ÁÏGCÏ߳̽øÐи÷Ïî²Ù×÷£¨°üÀ¨¼ì²â¶ÔÏó״̬¡¢ÊͷŶÔÏóÄÚ´æ¡¢µ÷ÓöÔÏóµÄ'finalize·½·¨£©µÄʱ»ú¡£ËäÈ»¿ÉÒÔͨ¹ýSystem.gc()ºÍRuntime.gc()º¯ÊýÌáÐÑGCÏ߳̾¡¿ì½øÐÐÀ¬»ø»ØÊÕ²Ù×÷£¬µ«ÊÇÕâÒ²ÎÞ·¨±£Ö¤GCÏß³ÌÂíÉϾͻá½øÐÐÏàÓ¦µÄ»ØÊÕ²Ù×÷¡£
¡¡¡¡ÄÚ´æй¶
¡¡¡¡ÄÚ´æй©ָÓÉÓÚ´íÎóµÄÉè¼ÆÔì³É³ÌÐòδÄÜÊÍ·ÅÒѾ²»ÔÙʹÓõÄÄڴ棬Ôì³É×ÊÔ´ÀË·Ñ¡£GC»á×Ô¶¯ÇåÀíʧȥÒýÓõĶÔÏóËùÕ¼ÓõÄÄÚ´æ¡£µ«ÊÇ£¬ÓÉÓÚ³ÌÐòÉè¼Æ´íÎó¶øµ¼ÖÂijЩ¶ÔÏóʼÖÕ±»ÒýÓã¬ÄÇô½«»á³öÏÖÄÚ´æй©¡£
¡¡¡¡±ÈÈçÏÂÃæµÄÀý×Ó¡£Ê¹ÓÃÊý×éʵÏÖÁËÒ»¸öÕ»£¬ÓÐÈëÕ»ºÍ³öÕ»Á½¸ö²Ù×÷¡£
¡¡¡¡import com.sun.javafx.collections.ElementObservableListDecorator;import com.sun.swing.internal.plaf.metal.resources.metal_sv;import java.beans.ExceptionListener;import java.util.EmptyStackException;/** * Created by peng on 14-9-21. */public class MyStack { private Object[] elements; private int Increment = 10; private int size = 0; public MyStack(int size) { elements = new Object[size]; } //ÈëÕ» public void push(Object o) { capacity(); elements[size++] = o; } //³öÕ» public Object pop() { if (size == 0) throw new EmptyStackException(); return elements[--size]; } //Ôö¼ÓÕ»µÄÈÝÁ¿ private void capacity() { if (elements.length != size) return; Object[] newArray = new Object[elements.length + Increment]; System.arraycopy(elements, 0, newArray, 0, size); } public static void main(String[] args) { MyStack stack = new MyStack(100); for (int i = 0; i < 100; i++) stack.push(new Integer(i)); for (int i = 0; i < 100; i++) { System.out.println(stack.pop().toString()); } }}
¡¡¡¡Õâ¸ö³ÌÐòÊÇ¿ÉÓõģ¬Ö§³Ö³£ÓõÄÈëÕ»ºÍ³öÕ»²Ù×÷¡£µ«ÊÇ£¬ÓÐÒ»¸öÎÊÌâûÓд¦ÀíºÃ£¬¾ÍÊǵ±³öÕ»²Ù×÷µÄʱºò£¬²¢Ã»ÓÐÊÍ·ÅÊý×éÖгöÕ»ÔªËصÄÒýÓã¬Õâµ¼Ö³ÌÐò½«Ò»Ö±±£³Ö¶ÔÕâ¸öObjectµÄÒýÓ㨴ËobjectÓÉÊý×éÒýÓã©,GCÓÀÔ¶ÈÏΪ´Ë¶ÔÏóÊÇ¿É´¥¼°µÄ£¬Ò²¾Í¸ü¼Ó̸²»ÉÏÊÍ·ÅÆäÄÚ´æÁË¡£Õâ¾ÍÊÇÄÚ´æй©µÄÒ»¸öµäÐÍ°¸Àý¡£Õë¶Ô´Ë£¬Ð޸ĺóµÄ´úÂëΪ£º
¡¡¡¡//³öÕ» public Object pop() { if (size == 0) throw new EmptyStackException(); Object o = elements[--size]; elements[size] = null; return o; }
¡¾JavaÀ¬»ø»ØÊÕ»úÖÆÒÔ¼°ÄÚ´æй©ÎÊÌâÂÛÎÄ¡¿Ïà¹ØÎÄÕ£º
¡¶À¬»øµÄ»ØÊÕÀûÓá·µÄ˵¿Î¸å06-06
ʲôÊÇÀ¬»ø·ÖÀà»ØÊÕ02-08
¿É»ØÊÕÀ¬»ø°üÀ¨ÄÄЩ11-06
ÎïÁ÷²Ö´¢¹ÜÀí´æÔÚµÄÎÊÌâÓë»úÖÆÓÅ»¯ÂÛÎÄ06-24
¹ØÓÚ»ØÊÕÀ¬»øµÄÊÖ³±¨02-18
À¬»ø»ØÊÕÓë·ÖÀàÊÖ³±¨04-02