要在程序里着手,看是否最优化。
http://www.onjava.com/lpt/a//onjava/2001/05/30/optimization.html

解决方案 »

  1.   

    java应该不会内存泄漏因为有垃圾收集机制,而起java也没有指针,如果内存泄漏,那么只能是sun的jvm的bug,自己是没法解决的
      

  2.   

    the memory leak discussed everywhere is normally because you keep the reference to an unused object somewhere.
    most obvious example would be:
    obj = new XXX();
    vector.add(obj);
    obj = null;//here, obj won't be gc'ed because it is still in vector.
      

  3.   

    java有自动垃圾收集机制,同时swing是轻量组件,内存泄漏应该不大,
    内存泄漏主要存在于极个别awt重量组件,但也不是全部
      

  4.   

    谢谢楼上各位,我发现了一个问题,就是我的组件都包含在一个JPanel容器里,而这个容器被我声明为static的了,但是容器内部的组件却不是,我想有可能是这个问题。
      

  5.   

    跟static声明应该没有必然关系。“内存泄漏”这个词在Java里并不确切,因为它的垃圾收集(内存释放)机制跟Windows内存管理器的完全不同。在Java中,并不是每一个对象在失效之后就会被立即释放掉所占内存。及时是Java的应用整体结束之后,也许很多对象仍可以在JVM中存在相当长时间——直至JVM下一次调用垃圾收集释放以上内存。Java应用程序对内存的冠里也不是完全被动的,你也可以在合适的地方显式地调用System.gc(),来让JVM立即清理无用的对象。比如,在paint()的结尾——因为画图的资源相对而言比较“昂贵”!更多关于Java垃圾收集问题请参考Thinking in Java 2nd Edition。
      

  6.   

    gc() does not "立即清理无用的对象".
    what it does is just say :"hey, I'm concerned about the memory, could you please check if you can collect any object for me?"
    But, jvm does not guarantee to do anything for you. It may say:"I'm sorry, but I don't see any necessity to do that now."tainy_zhang:
    if you like, you can post some suspicious code, so that we can find more clue.
      

  7.   

    我认为这个是内存泄露主要是因为:
    我用optimizeit和purify都进行了测试,在打开一个窗口之前和关闭一个窗口之后都利用工具提供的gc进行了垃圾清除,可以发现系统的确做了gc,因为free memeory增加了,可是,针对某些窗口的测试表明,这个窗口在开关一次后,分配的内存总是有一个固定数量不能被回收,比如每次都是500k左右,所以我觉得是内存泄露。
    在solaris上运行的时候,可以观察free mem在持续下降,并且有时会导致GUI灰掉并且不可用,这个问题非常严重,所以特别急切的希望大家的帮助,谢谢!!about the code:we develop large app,so I think it is impossible for you to read the whole code.but piece of it show nothing,I think.thanks anyway
      

  8.   

    well. without code, we can barely say anything useful.
    the gc is most unlikely to have a bug.
    some gui api may have problem though.some advices:
    1. gc() does not help. if you find memory leak, go find the reason. gc() will not do any good to it.
    2. investigate your code. for things like
    {
    Object obj = ...;
    somefunction(obj);
    }
    //obj may not be collected by gc here
    3. 
    {
    String s = get_a_big_string()
    String s2 = s.substring(i, j);
    somefunc(s2);
    }
    //here, s will not be collected until s2 is collected.
    4. some async image api may require substantial memory, but takes time to release it. if you keep calling async image api, you may run out of memory. (this is not called memory leak though).
      

  9.   

    sorry,the code is too long to post here in one post or two ,so I want to talk to you by email,can you tell me your address?
    By the way,the most serious thing is not the memory leak,but the app Gui may become gray and unuseable on Solaris running under JRE1.2.2 which will not happen under win2000.
    For some reason,we developped the app using 1.3.1,but have to compile and run it under 1.2.2 now;-(.Do you know why the app will turn gray?
      

  10.   

    did you use awt or [email protected]