国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

使用jprofile發(fā)現(xiàn)和修復(fù)內(nèi)存泄露

2019-11-15 00:09:08
字體:
供稿:網(wǎng)友
使用jPRofile發(fā)現(xiàn)和修復(fù)內(nèi)存泄露

性能分析有一項(xiàng)是:發(fā)生OOM時(shí),瀏覽對(duì)象分配和引用以發(fā)現(xiàn)和修復(fù)內(nèi)存泄露;

示例程序PointFactory

public class PointFactory {    protected ArrayList points = new ArrayList();    protected static PointFactory instance = new PointFactory();    public Point createPoint(int x, int y) {        Point point = new Point(x, y);        this.points.add(point);        return point;    }    public void removePoint(Point point) {        this.points.remove(point);    }    public void printTestPoints() {        for (int i = 0; i < 5; i++) {            Point point = createPoint(i, i);            System.out.println("Point = " + point);        }    }    public static PointFactory getInstance() {        return instance;    }    public static void main(String[] args) throws Exception {        JFrame frame = new JFrame("Points Test");        frame.setDefaultCloSEOperation(WindowConstants.EXIT_ON_CLOSE);        JButton button = new JButton("Print Test Points");        button.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent e) {                PointFactory.getInstance().printTestPoints();            }        });        frame.getContentPane().add(button);        frame.setSize(200, 100);        frame.setVisible(true);    }}
View Code

運(yùn)行PointFactory;

clip_image001[8]

運(yùn)行jprofile,選擇本地jvm;

clip_image002[5]

選擇程序;

clip_image003[4]

來了個(gè)很恐怖的警告:

clip_image004[4]

某些情況下,在attach mode下有一個(gè)bug會(huì)導(dǎo)致jvm崩潰;

Sun JVM Attach API是Sun JVM中的一套非標(biāo)準(zhǔn)的可以連接到JVM上的API;

Bug詳情:

  • Sun java (HotSpot) client JVM can crash in attach mode due to a JVM bug Due to a JVM bug 6776659 HotSpot client JVM can crash in attach mode. There is no crash for the server JVM: JVM option -server solves the problem.

源文檔 <https://www.yourkit.com/docs/java/help/attach_agent.jsp>

咱還是聽話運(yùn)行在server mode吧;

clip_image005[4]

這次就沒提示了;

選擇

clip_image006[4]

選擇instrumentation儀表盤,我們要看所有的分析;

clip_image007[4]

這么多類咋看嘛,懷疑Point類存在內(nèi)存泄露,那就只看它了,設(shè)置View Filters;

clip_image008[4]

這時(shí)候還沒有點(diǎn)按鈕,所以沒有創(chuàng)建一個(gè)類;點(diǎn)!

clip_image009[4]

clip_image010[4]

但是視圖里邊還是空啊;

得用java.awt.Point,幸好我夠機(jī)智;

clip_image011[4]

為啥5個(gè)類出來total是333?

再點(diǎn)一次,變成418;

clip_image012[4]

點(diǎn)擊垃圾回收Run GC:

clip_image013[4]

剩10個(gè)實(shí)例,正常了;本應(yīng)10個(gè)實(shí)例,為什么會(huì)有418個(gè)那么多;

以此類推,點(diǎn)按鈕,Run GC,然后total每點(diǎn)一次增加5個(gè),證明了Point類是回收不掉的;

已經(jīng)集成到eclipse的話,可以Profile As Java application,很方便;

clip_image014[4]

那為啥Point類沒釋放呢?明明它已經(jīng)沒用了;

實(shí)際項(xiàng)目中,找源代碼然后逐個(gè)找引用會(huì)累死人,咱還是通過運(yùn)行時(shí)堆棧的快照來看吧;

切換到Heap視圖;

clip_image015[6]

在累計(jì)引用列表可以看到誰引用了Point;

clip_image015[7]

這里是points這個(gè)Arraylist引用了,但是沒有移除導(dǎo)致;

使用完之后調(diào)用removePoint就可以了。


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 新邵县| 昂仁县| 兴安盟| 婺源县| 普安县| 南通市| 澄迈县| 呼和浩特市| 密山市| 萨迦县| 姚安县| 台中县| 鸡东县| 馆陶县| 昌宁县| 田林县| 庄河市| 宁阳县| 无极县| 崇左市| 昌江| 东兰县| 藁城市| 新河县| 平江县| 平罗县| 梅河口市| 芒康县| 新巴尔虎左旗| 鄢陵县| 攀枝花市| 彰化县| 临澧县| 长治市| 深州市| 会昌县| 手机| 长岭县| 海晏县| 乐平市| 威信县|