How to view the current heap size that an applicat

2019-01-10 08:06发布

I think I increased my heap size to 1 GB in NetBeans since I changed the config to look like this:

netbeans_default_options="-J-Xmx1g ......

After I restarted NetBeans, can I be sure that my app is given 1 GB now?

Is there a way to verify this?

7条回答
家丑人穷心不美
2楼-- · 2019-01-10 08:34
public class CheckHeapSize {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        long heapSize = Runtime.getRuntime().totalMemory(); 

        // Get maximum size of heap in bytes. The heap cannot grow beyond this size.// Any attempt will result in an OutOfMemoryException.
        long heapMaxSize = Runtime.getRuntime().maxMemory();

         // Get amount of free memory within the heap in bytes. This size will increase // after garbage collection and decrease as new objects are created.
        long heapFreeSize = Runtime.getRuntime().freeMemory(); 

        System.out.println("heapsize"+formatSize(heapSize));
        System.out.println("heapmaxsize"+formatSize(heapMaxSize));
        System.out.println("heapFreesize"+formatSize(heapFreeSize));

    }
    public static String formatSize(long v) {
        if (v < 1024) return v + " B";
        int z = (63 - Long.numberOfLeadingZeros(v)) / 10;
        return String.format("%.1f %sB", (double)v / (1L << (z*10)), " KMGTPE".charAt(z));
    }
}
查看更多
走好不送
3楼-- · 2019-01-10 08:39

Attach with jvisualvm from Sun Java 6 JDK. Startup flags are listed.

查看更多
成全新的幸福
4楼-- · 2019-01-10 08:42

For check your java application heap size and CPU time use JProfiler tools that will access your JVM and display all heap and CPU time allocation

plug in available for eclipse and intellej Idea IDE

Link for Download Jprofiler

http://www.ej-technologies.com/download/jprofiler/files

查看更多
倾城 Initia
5楼-- · 2019-01-10 08:50

Use this code:

// Get current size of heap in bytes
long heapSize = Runtime.getRuntime().totalMemory(); 

// Get maximum size of heap in bytes. The heap cannot grow beyond this size.// Any attempt will result in an OutOfMemoryException.
long heapMaxSize = Runtime.getRuntime().maxMemory();

 // Get amount of free memory within the heap in bytes. This size will increase // after garbage collection and decrease as new objects are created.
long heapFreeSize = Runtime.getRuntime().freeMemory(); 

It was useful to me to know it.

查看更多
smile是对你的礼貌
6楼-- · 2019-01-10 08:51

You can use jconsole (standard with most JDKs) to check heap sizes of any java process.

查看更多
啃猪蹄的小仙女
7楼-- · 2019-01-10 08:54

Personal favourite for when jvisualvm is overkill or you need cli-only: jvmtop

JvmTop 0.8.0 alpha   amd64  8 cpus, Linux 2.6.32-27, load avg 0.12
https://github.com/patric-r/jvmtop

PID MAIN-CLASS      HPCUR HPMAX NHCUR NHMAX    CPU     GC    VM USERNAME   #T DL
3370 rapperSimpleApp  165m  455m  109m  176m  0.12%  0.00% S6U37 web        21
11272 ver.resin.Resin [ERROR: Could not attach to VM]
27338 WatchdogManager   11m   28m   23m  130m  0.00%  0.00% S6U37 web        31
19187 m.jvmtop.JvmTop   20m 3544m   13m  130m  0.93%  0.47% S6U37 web        20
16733 artup.Bootstrap  159m  455m  166m  304m  0.12%  0.00% S6U37 web        46
查看更多
登录 后发表回答