Swing


天行健 君子以自强不息

posts - 69, comments - 215, trackbacks - 0, articles - 16
   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

Java中取JVM中内存的方法

Posted on 2008-04-18 14:10 zht 阅读(668) 评论(4)  编辑  收藏 所属分类: J2SE

  Runtime runtime = Runtime.getRuntime();
  long total = runtime.totalMemory();

  long free = runtime.freeMemory();
  System.out.println(total+"-"+free);


totalMemory()
    /**
     * Returns the total amount of memory in the Java virtual machine.
     * The value returned by this method may vary over time, depending on
     * the host environment.
     * <p>
     * Note that the amount of memory required to hold an object of any
     * given type may be implementation-dependent.
     *
     * @return  the total amount of memory currently available for current
     *          and future objects, measured in bytes.
     */
freeMemory()
    /**
     * Returns the amount of free memory in the Java Virtual Machine.
     * Calling the
     * <code>gc</code> method may result in increasing the value returned
     * by <code>freeMemory.</code>
     *
     * @return  an approximation to the total amount of memory currently
     *          available for future allocated objects, measured in bytes.
     */

通过这个方法,可以写一个类似于Windows任务管理器的面板: