Java Memory Optimization Test, Memory Optimization Test

Source: Internet
Author: User
Tags jconsole

Java Memory Optimization Test, Memory Optimization Test

After two years of development in c ++, he was transferred to the java project last month. So the first article was about java memory optimization.

First of all, optimization should be done at the end, but it is also important to develop good habits in the process of writing code. First, I would like to recommend a book "Writing high-quality code: 151 suggestions for improving Java programs. Qin Xiaobo".

First, when writing code, use as few objects as possible, and replace basic variables with basic variables. The following is an example.

Secondly, you often want to develop a function to write a piece of code, either by changing the time for space or by changing the time for space. Based on whether this function is time-oriented or space-oriented, it is usually stored in the memory, but whether it can be compressed depends on whether it has an impact on efficiency.

I believe everyone has their own good habits.

I mainly want to talk about memory optimization.

Because the current project needs to parse a large amount of data and save it, it is very important to save memory. Otherwise, importing a MB file will occupy 1 GB of memory, and the customer will be crazy.

Therefore, Xiao yuantakes the first step to check the memory usage.

Jconsole.exe is a tool provided by JDK.

With this software, you can clearly see the memory, CPU, and thread of your program.

At the beginning, he found that the amount of memory used by his program heap was not that large. How did he add up to be different from that of the task manager! Then, the program's eden space occupies a small amount. It turns out that the eden space parameter is not set. You need to set it in the startup configuration file:-Xmn,-XX: NewSize,-XX: MaxNewSize,-XX: you can set NewRatio based on your needs.

Because if no force variable is applied directly to the old gen, the variable is first applied to the eden gen, and then after gc, if the variable survive, it enters the same vor area, after several gc operations, the variable exists in the old gen zone.

In fact, after the program starts stable, most of the variables may have been transferred to the old gen zone. If your eden zone memory allocation is too large, the total amount minus vor minus eden will be insufficient for the remaining old zone. What is the virtual machine doing in this case? The virtual opportunity will automatically scale up based on your-Xms and-Xmx settings, as a result, many of the system memory you actually obtain from the virtual machine is idle memory, which causes the memory displayed in the task manager to be much larger than what you actually use.

As a result, the startup parameter was completed at the first step, and the memory consumption was reduced. However, this is an illusion that the actual memory occupied by the virtual machine remains unchanged.

John wants to know more about memory usage, which requires the MemoryAnalyzer tool. But before using this tool, he must generate the program's dump file, my Baidu method is to add the startup parameter + HeapDumpOnOutOfMemoryError and manually add an exception OutOfMemory to generate the dump file.

Okay, the ape is abnormal, and the camera is down.

Finally, we found a good method. In fact, jdk's built-in jconsole can be generated.

In MBean-> com. sun. management-> HotSpotDiagnostic-> operation-> dumpHeap, change the p0 value to the path where the dump file is generated. Click dumpHeap. But the suffix of the dump file is not dump !!! It's hprof !!!

After generation, use MemoryAnalyzer to open it. After leak Suspects is generated, a magic pie chart is displayed. It shows the memory occupied by the current instance. There is a details, click here.

Do you have jdk installed on the Child paper that cannot open MemoryAnalyze? Are java environment variables ready.

The detailed memory usage information, number of objects, and so on are displayed.

Now I finally found out what can be optimized.

Previously, I had a hard time complaining about java's String. This is a deeper resentment, that is, the previous sentence. The basic type can be used as the basic type.

Ape found that there are 100000 String objects and 100000 char [] objects.

Well, you can use a String to store a String. In fact, you can use a char to store a String in it. Then, he also has his own relatives.

You save a "0", and the size of the object given by String is much larger than that given by 1 byte!

So you can call the toCharArray () method of String to convert it to char [] and save it.

Here we will explain when to use String, StringBuffer, and char.

In fact, it is excellent to define a common constant String and use String.

For example

String strTemp = "01 ";

String strTemp2 = "01 ";

In this definition, strTemp and strTemp2 actually share an object, but the reference of this object is 2!

Therefore, defining 10000000000 objects only occupies a String object, which is a constant pool unique to the String object.

Why should we use StringBuffer and StringBuild?

Because the append of StringBuffer and StringBuild is better than the String +!

In addition, the String itself is designed according to constants, and StringBuffer and StringBuild are truly changeable strings.

However, if the program needs to save a large number of strings without rules, it is recommended to convert them to char for storage.

This is only a string type, and other int types are also the principle. Try to save it with basic variables.

Looking at the high-end java, we claim that there is no memory leakage of java. If we use it improperly, it will cause a waste of memory.

Although the memory will be correctly released at the moment of exiting the program, sometimes we are more concerned about the memory usage in the running process.

As long as the reference count of a variable is not 0, gc will not be able to recycle it. Maybe this object is useless for the moment, but it will be added to an arraylist to be released after the program ends, this is a waste of operations.

Java does not expose memory, but the release time is also very important. An object is useless for us, but the key of this object was accidentally lent to an object with a longer life cycle, which is memory leakage for us.

Let's take a good look at MemoryAnalyze and analyze whether all existing objects are actually useful. If any, it must be referenced.

He finally completed the first java essay. A month after the speed of java is reached, it will be converted to html5 next month. Do not read ..............................

 

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.