rednight

          0x2B|~0x2B,That's not a question,Just do it.
          posts - 32, comments - 14, trackbacks - 0, articles - 0

          Memory Leak for ListCellRenderer

          Posted on 2007-01-16 09:04 rednight 閱讀(437) 評論(0)  編輯  收藏

          We have known, the ListCellRenderer interface has only one method public Component getListCellRendererComponent(), and it returns a Component. When a JList needs to compute its dimension or paint its cell item, this method will be called. So it will be called frequently, and we have a best practice, try to reuse object, avoid time-consumed computation and unnecessary operation in this method.

          ?

          Recently found, there is a memory leak bug for list cell renderer in both JDK 1.4.x and JDK 5 (don’t know whether has the same bug in earlier JDK). The component returned from getListCellRendererComponent() method could not be GC, and all its referenced objects also could not be GC. Unfortunately, this bug is only fixed in JDK 6.

          ?

          So currently, if a JList use the ListCellRenderer, but only holds a little resource (eg: the JList only has little items, and the component is returned from ListCellRenderer is a simple JLable), you may not care about the memory leak.

          ?

          But if a JList use the ListCellRenderer, and holds a large resource, you should remember to use the following way to avoid memory leak.

          ?

          Work Around: Subclass JList and invoke removeAll after painting is done:

          ?????? public class xxxList extends JList {

          ??????????? private CellRendererPane renderer;

          ??????????? private CellRendererPane getRenderer() {

          ??????????????? if (renderer == null) {

          ??????????????????? for (int i=0; i<getComponents().length; i++) {

          ????????????????????????????????????????? Component c? = getComponents()[i];

          ??????????????????????? if (c instanceof CellRendererPane) {

          ??????????????????????????? renderer = (CellRendererPane)c;

          ??????????????????????????? break;

          ??????????????????????? }

          ??????????????????? }

          ??????????????? }

          ??????????????? return renderer;

          ??????????? }

          ??????????? protected void paintComponent(Graphics g) {

          ??????????????? super.paintComponent(g);

          ??????????????? CellRendererPane renderer = getRenderer();

          ??????????????? if (renderer != null) {

          ??????????????????? renderer.removeAll();

          ??????????????? }

          ??????????? }

          ??????? }


          Please refer to http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5044798 for more details.

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 新野县| 双辽市| 滁州市| 赫章县| 融水| 泰宁县| 静海县| 苍山县| 尼木县| 长治市| 五常市| 无棣县| 高陵县| 东海县| 东阳市| 桦甸市| 麦盖提县| 凉城县| 乳山市| 肇州县| 长治县| 定结县| 剑阁县| 林州市| 定襄县| 若尔盖县| 德清县| 盐池县| 淮北市| 米脂县| 榆树市| 万山特区| 辉县市| 纳雍县| 河间市| 东至县| 兴宁市| 江津市| 沧源| 林甸县| 峡江县|