Xzajo's Weblog

All | Java | nbxdoclet | NetBeans | Personal | Project Extensions | Sun | Testing
« XTest testing framew... | Main | GWT - revolution in... »
20060531 Wednesday May 31, 2006

Testing memory leaks with assertGC

I was talking about assertSize in one of previous entry about testing. NbTestCase class contains more useful methods. Today I will be talking about assertGC method. The assertGC method tests tries to run garbage collect with different methods and then scan if exists path to reference in memory by using insane library. Look at simple example:

public class LeakTest extends NbTestCase {
    
    public LeakTest(java.lang.String testName) {
        super(testName);
    }
    public void testLeak() {
        String obj = new String("test1");
        WeakReference ref = new WeakReference(obj);
        obj = null; 
        assertGC("pass",ref);
        obj = new String("test2");
        ref = new WeakReference(obj);
        assertGC("fails",ref);
    }
    
    public static Test suite() {
        TestSuite suite = new NbTestSuite(LeakTest.class);
        return suite;
    }
}

First asertGC passed because in JVM exists only WeakReference for object String("test1"). The second assertGC failed because the reference obj holds String("test2").

Posted by xzajo ( May 31 2006, 02:46:35 PM CEST ) Permalink Comments [16]

Comments:

Will AssertGC produce any output, or is there any API from INSANE that will report what objects are holding references after an AssertGC?

It would be nice to see who's holding a reference to what objects before and after a GC. Or, even better; do a GC, make a marking point, do some work, do several GC's (for finalizers), then report any new object references created in the "do some work" step.

And, how does INSANE or assertGC() deal with objects with finalizers and objects which may be queued up in the finalizer queue, (i.e. take more than 1 GC to be collected) ?

Posted by huntch on June 05, 2006 at 08:54 PM CEST #

Some issues are coming to my mind when reading your sample test case. First thing I find suboptimal is the need for hard-encoding a text ("pass","fail") as a parameter with assertGC. Why not offer two methods (assertGCPasses, assertGCFails resp. assertNotGC)? I didn't look into the NBJUnit code, maybe it's there already, but then you should have used it in your sample. Second thing is the title of your blog entry. It makes the reader assume that assertGC is able finding memory leaks within any application without further requirements. As I understand after reading a second time, this is not the case because assertGC tests if the GC strategy used worked properly. Did I understand that correctly? Third aspect is that I like the idea of assertGC! Klaus

Posted by Klaus Meffert on June 08, 2006 at 10:56 AM CEST #

Yes, it takes more GC. assertGC() method implementation:
 public static void assertGC(String text, Reference<?> ref, Set<?> rootsHint) {
        List<byte[]> alloc = new ArrayList<byte[]>();
        int size = 100000;
        for (int i = 0; i < 50; i++) {
            if (ref.get() == null) {
                return;
            }
            System.gc();
            System.runFinalization();
            try {
                alloc.add(new byte[size]);
                size = (int)(((double)size) * 1.3);
            } catch (OutOfMemoryError error) {
                size = size / 2;
            }
            try {
                if (i % 3 == 0) Thread.sleep(321);
            } catch (InterruptedException t) {
                // ignore
            }
        }
        alloc = null;
        fail(text + ":\n" + findRefsFromRoot(ref.get(), rootsHint));
    }

Posted by xzajo on June 08, 2006 at 01:53 PM CEST #

OK, I see. The text is just displayed to the user. But then there should also be the negative form of assertGC, something named assertNotGC (which fails in case a garbage collection runs). Is something like that there?

Posted by Klaus Meffert on June 08, 2006 at 05:50 PM CEST #

I am not sure if assert notGC will be usefull. You can use:

assertNotNull(ref)

Posted by xzajo on June 09, 2006 at 03:06 PM CEST #

wow power leveling wow power leveling wow powerleveling wow powerleveling 搬家公司

Posted by 水无痕 on July 21, 2007 at 06:48 AM CEST #

<a href="http://www.feelingame.com/">wow power leveling</a>
<a href="http://www.feelingame.com">wow powerleveling</a>
<a href="http://www.cheap-wow-power-leveling.com">cheap wow power leveling</a>
<a href="http://www.cheap-wow-power-leveling.com/">wow power leveling</a>
<a href="http://www.cheap-wow-power-leveling.com/">wow powerleveling</a>
<a href="http://www.cheap-wow-power-leveling.com/">wow power lvl</a>

Posted by 00000000 on August 22, 2007 at 09:21 AM CEST #

http://www.wowgolds.co.uk wow gold http://www.wowgolds.co.uk cheap wow gold http://www.wowgolds.co.uk buy wow gold http://www.wowgolds.co.uk world of warcraft gold http://www.wowgolds.co.uk wow http://www.wowgolds.co.uk world of warcraft http://www.wowgolds.co.uk/wow-gold.html wow gold http://www.wowgolds.co.uk/warrior.html WoW Warrior http://www.wowgolds.co.uk/hunter.html WoW Hunter http://www.wowgolds.co.uk/rogue.html WoW Rogue http://www.wowgolds.co.uk/paladin.html WoW Paladin http://www.wowgolds.co.uk/shaman.html WoW Shaman http://www.wowgolds.co.uk/priest.html WoW Priest http://www.wowgolds.co.uk/mage.html WoW Mage http://www.wowgolds.co.uk/druid.html WoW Druid http://www.wowgolds.co.uk/warlock.html WoW Warlock http://www.powerleveling2.cn power leveling http://www.powerleveling2.cn powerleveling http://www.wowpowerleveling2.cn wow power leveling http://www.wowpowerleveling2.cn wow powerleveling http://www.wowpro.cn wow guides http://www.wowmmo.cn wow tips http://ggtop.nbatop.com google排名 http://ggtop.nbatop.com google左侧排名 http://ggtop.nbatop.com google排名服务 http://www.baidupaiming.net 百度推广 http://www.baidupaiming.net 百度排名 http://www.fzqsnet.cn 网站推广 http://www.thebiz8.com 商业吧 http://www.thebiz8.com/jichuang.html 机床 http://www.thebiz8.com/leddeng.html LED灯 http://www.thebiz8.com/dianchi.html 电池 http://www.thebiz8.com/suliao.html 塑料 http://www.thebiz8.com/shexiangji.html 摄像机 http://www.thebiz8.com/yimin.html 移民 http://www.thebiz8.com/jiachun.html 甲醇 http://www.thebiz8.com/ranliao.html 染料 http://www.0591qs.cn 福州热线 http://www.sportblog.org.cn 体育博客 http://www.stockblog.org.cn 股票博客 http://www.gameblog.org.cn 游戏博客 http://www.cheap-wow-gold.org.cn 魔兽博客 http://www.excamtest.cn 考试博客 http://www.carblog.org.cn 汽车博客 http://www.houseblog.org.cn 房产博客 http://www.pcblog.org.cn 电脑博客 http://powerlin518.iblog.com powerlin518 http://www.xcelwebdesign.com logo design http://www.xcelwebdesign.com website design http://www.xcelwebdesign.com web design http://www.shangbiaodesign.cn 商标设计

Posted by power leveling on March 20, 2008 at 04:39 PM CET #

<a href="http://blog.tyfo.com/?U=banjiagg" rel="nofollow">搬家公司</a>
<a href="http://blog.tyfo.com/?U=banjiagg">搬家公司</a>

Posted by hrz on April 06, 2008 at 09:06 AM CEST #

<a href="http://blog.tyfo.com/?U=banjiagg" rel="nofollow">搬家公司</a>
<a href="http://blog.tyfo.com/?U=banjiagg">搬家公司</a>

Posted by hrz on April 06, 2008 at 09:07 AM CEST #

<a href="http://blog.tyfo.com/?U=banjiagg" rel="nofollow">搬家公司</a>
<a href="http://blog.tyfo.com/?U=banjiagg">搬家公司</a>
<a href="http://www.hrzbq.com">深圳搬家公司</a>

Posted by hrz on April 06, 2008 at 09:08 AM CEST #

Landseer

Posted by wow gold on October 27, 2008 at 01:31 AM CET #

you can buy cheap wow gold.

Posted by wow gold on October 27, 2008 at 01:32 AM CET #

buy warhammer gold.

Landseer

Posted by Warhammer gold on October 27, 2008 at 01:34 AM CET #

http://sunnyren.weblog.com/
this blog introduces jewelry products and fashion jewelry

Posted by fashion jewelry on October 31, 2008 at 06:50 AM CET #

Post a Comment:

Comments are closed for this entry.

Calendar

RSS Feeds

Search

Links

Navigation

Referers