The things that JAVA memory

Source: Internet
Author: User
Tags xms

While Java masks memory details, sometimes it's good to know about them, especially in the oral exams, which always stare at these things and don't let go.

After the JVM is started, two types of memory areas are allocated, one for development staff, such as saving variables, objects, and so on, which are used by a class of JVMs, such as storing some class classes and descriptions.

1, the first type of memory area can be divided into stacks (stack), heap (heap), there are some static storage areas, this part of the memory when the JVM starts, can be configured with parameters:

-xms Initial heap size, this value should not be too small, its initial space (that is,-XMS) is the physical memory of 1/64, this value can not be too small, such as set up-xms1m, run may appear

Error occurred during initialization of VMS

Too Small initial heap for new size specified

-xmx Heap size limit, maximum space (-XMX) is the physical memory of 1/4, if the program allocated memory exceeds this limit, then will appear

Exception in thread "main" Java.lang.OutOfMemoryError:Java heap space

The code is:

Byte[] B = new byte[100000000];

-XSS thread stack size, generally not set, JDK5.0 after each thread stack size is 1M, the previous thread stack size is 256K. The size of the memory required for the more applied threads to be adjusted. Sometimes you find something unusual,

Exception in thread "main" Java.lang.StackOverflowError

The reasons are generally:

public static int callmyself () {

return callmyself ();

}

Recursive or dead loop of the method, resulting in insufficient stack space.

Stack and heap What to save, a lot of places are mentioned, here refer to the "Think in Java", the stack contains object references, basic types of variables, and the heap contains objects and arrays. The execution of the method is performed on the stack, which can pass the exception, often by default printing

E.printstacktrace ();

stack information to know.

The runtime class has several functions, and we can simply take a look at some of the memory information in the JVM through these functions.

MaxMemory () This method returns the maximum amount of memory that the Java Virtual machine (this process) can construct from the manipulation system, in bytes, if the Java program is running without adding the-XMX parameter, then it is 64 trillion, which means maxmemory () The return is approximately 64*1024*1024 bytes, which is the maximum memory that the Java Virtual machine can dig from the manipulation system by default. If the-XMX parameter is added, the value following this parameter will prevail, such as JAVA-CP classpath-xmx512m ClassName, then the maximum memory is 512*1024*0124 bytes.

TotalMemory () This method returns the amount of memory that the Java Virtual machine has now dug up from the manipulation system, that is, all the memory occupied by the Java Virtual machine process at that time. If you do not add-XMS parameters when running Java, then, in the Java program running process, memory is always slowly from the control system dug, basically is how much digging, straight to maxmemory () so far, so totalmemory () is slowly increasing. If the-XMS parameter is used, the program will start with unconditional digging from the control system-the amount of memory defined by XMS, and then digging when the memory is contempoly.

Freememory () is what, just talked about if you run Java without adding-XMS parameters, then, in the Java program running process, memory is always slowly from the operating system dug, basically is how much digging how much, But Java Virtual machine 100% in the case of a little bit more digging, these dug and no use of memory, is actually freememory (), so the value of freememory () is generally very small, However, if you are running a Java program using-XMS, this time because the program at the start of the process will be unconditionally dug from the control system-XMS the number of memory defined later, this time, the memory dug up may be mostly useless, so this time freememory () may be a bit large.

Let's take a look at the examples below:

Runtime RT = Runtime.getruntime ();

Info ("Max Memory:" + rt.maxmemory ());

Long fisrt = Rt.freememory ();

Info ("Total Memory:" + rt.totalmemory ());

Info ("Free Memory:" + fisrt);

int size = 10000;

Byte[] B = new Byte[size];

Long BL = Rt.freememory ();

Info ("Free Memory:" + BL);

Info ("Byte Allocate cost Memory:" + (FISRT-BL) + ", Array size:" + size);

The Run parameter is-xms8m-xmx32m (too large to be seen) and the result is:

2011-02-22 10:28:01:max memory:33357824

2011-02-22 10:28:01:total memory:8323072

2011-02-22 10:28:01:free memory:7791752

2011-02-22 10:28:01:free memory:7781736

2011-02-22 10:28:01:byte Allocate cost memory:10016, Array size:10000

33357824 <> 32*1025*1024 (about that)

8323072 <> 8x1024x1024

Finally look at the 10000-length byte array, allocating how much memory, about 10016, which shows that in addition to 10,000 bytes of size 1 bytes, there are 16 other things.

change byte to int (4 bytes):

2011-02-22 10:35:21:int Allocate cost memory:40016, Array size:10000

Same as Byte, also 4*10000+16

Change byte to Long (8 bytes):

2011-02-22 10:32:47:long Allocate cost memory:80016, Array size:10000

Same as Byte, also 8*10000+16

And look at the string array:

2011-02-22 10:34:40:string Allocate cost memory:40016, Array size:10000

String as an object that allocates the same amount of memory as an int, indicating that the machine is a 32 (4*8) bit

Finally, look at object objects,

2011-02-22 10:37:02:object Allocate cost memory:40016, Array size:10000

is the same as string.

2, the second type of memory, I understand the main is PermGen space, the full name is permanent Generation space, refers to the memory of the permanent storage area, this memory is mainly stored by the JVM class and meta information, Class is placed in PermGen space when it is loader, unlike the heap area where the class instance (Instance) is stored, Sun's JDK is not in the GC (garbage Collection) for PermGen in the main program run time Space for cleanup, so if you have a very class in your application, you are likely to have permgen space errors.

It turns out that the Sun's JVM has divided the memory into different areas, one of which is the Permenter area used to store very many classes and class descriptions. Sun was designed to think that this area was fixed when the JVM started, but he did not think that the dynamic would be used so extensively now. And this area has a special garbage recovery mechanism, now the topic is the dynamic loading class to this area, the GC has no way to recycle!

The things that JAVA memory

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.