Java Interview _ Memory Model

1. The so-called memory model
depends on whether you know the memory division of Java.
See if you have any specific work experience, because in actual development, if you do not adjust the memory, then performance will cause a great waste
of GC interpretation operations

If it comes to memory, perhaps the most intuitive understanding is in the Runtime class, then this class uses the singleton design pattern, which provides the acquisition of memory information and the system's garbage collection processing operations

Get the default value of Runtime:

public class TestRuntime {
public static void main(String [] args)
{
Runtime run = Runtime.getRuntime(); //取得Runtime类的对象
System.out.println("MAX_MEMORY = " + run.maxMemory());
System.out.println("TOTAL_MEMORY = " + run.totalMemory());
System.out.println("FREE_MEMORY = " + run.freeMemory());
}
}

result:

MAX_MEMORY = 932184064 is equivalent to 889M, the maximum bearing capacity
TOTAL_MEMORY = 64487424 is equivalent to 61.5M, the size of commonly used memory space
FREE_MEMORY = 62440328 is equivalent to 59.547M, involving some system initialization operations, will occupy some memory space

The return value of these three methods is long byte type, so it returns byte type.
In fact, in Javva, if you want to change the memory, you need to master the following memory structure (memory model)

In Java, the space of memory can be divided into the following points
: Eden Park: Newly born objects are stored here, but not necessarily these new born objects will always survive.
Here is also a memory space, so since it is a memory space, It will definitely be full, if it is full, then perform GC processing.
Runtime class has GC

旧生代区:如果对象发现其要一直使用, 那么就将进入到旧声带去, 这属于耳机回收保险
        如果要是先执行GC, 那么坑定先清理伊甸园去, 随后如果发现空间不足, 继续清理旧生代区
永久区:永久区中的数据不会清楚, 方法也在方法区中, 即使程序出现了"OutOfMemoryError"也不会清除

Observe memory intuitively:

import java.util.Date;

public class TestRuntime {
public static void main(String [] args)
{
Runtime run = Runtime.getRuntime(); //取得Runtime类的对象
System.out.println("1.MAX_MEMORY = " + run.maxMemory());
System.out.println("1.TOTAL_MEMORY = " + run.totalMemory());
System.out.println("1.FREE_MEMORY = " + run.freeMemory());

    System.out.println("****************************************");


    String str = "";
    for(int x=0; x< 100000; x++)
    {
        str += "";
        new Date();
    }


    str = null;     //产生了垃圾
    System.out.println("2.MAX_MEMORY = " + run.maxMemory());
    System.out.println("2.TOTAL_MEMORY = " + run.totalMemory());
    System.out.println("2.FREE_MEMORY = " + run.freeMemory());


    run.gc();       //进行垃圾回收处理


    System.out.println("3.MAX_MEMORY = " + run.maxMemory());
    System.out.println("3.TOTAL_MEMORY = " + run.totalMemory());
    System.out.println("3.FREE_MEMORY = " + run.freeMemory());




}

}

Adjust the memory size: that is, adjust the size of the stack Adjust
in VM options:
-Xms2048M -Xmx2048M -Xmn1024M
Initialize the memory space: 2 G, maximum allocation space 2 G Set a new space 1 G

Running case:

Runtime run = Runtime.getRuntime(); //取得Runtime类的对象
System.out.println("1.MAX_MEMORY = " + run.maxMemory());
System.out.println("1.TOTAL_MEMORY = " + run.totalMemory());
System.out.println("1.FREE_MEMORY = " + run.freeMemory());

结果:
1.MAX_MEMORY = 2013265920
1.TOTAL_MEMORY = 2013265920
1.FREE_MEMORY = 1964947496

Only after the adjustment, the subject will ensure that the system memory will be improved, so it is obvious that you can give full play to the performance of your computer, because the default size is 1/4 the size of the computer memory, and no more than 1 G,

-Xms: initial allocated memory size, default is 1/64 of physical memory, less than 1G
-Xmx: maximum allocated memory, default size is 1/4 of physical memory, less than 1G
-Xmn: set young generation (Eden Park) The size of the heap memory
Mavean can also increase the speed of compilation by setting the size of the memory parameters

Published 3 original articles · Likes0 · Visits 145

Guess you like

Origin blog.csdn.net/weixin_42694727/article/details/105591346