Java Memory optimization Kind

Source: Internet
Author: User
Tags jconsole

The little ape had been in C + + for two years and was transferred to the Java Project last month, so the first essay thought of 818 Java memory optimization.

The first optimization of this kind of thing, definitely should be put to the last to do, but in the process of writing code to develop good habits is also very important. Here we recommend a book, "Writing High-quality code: 151 Suggestions for improving Java programs." Qin Xiaobo.

First of all, when writing code, try to use less objects, you can use basic variables instead of the basic variables, this is an example below.

Second, many times you want to do a function, write a piece of code, not time to change space is to use space to change time. To be based on this function in the end is the fancy time, or fancy space, often access to the inevitable is to put into memory, but it is not possible to compress this also depends on the effectiveness of the impact.

Others do not say more, I believe you have their own good habits.

Mainly want to talk about memory optimization.

Small apes now do the project need to parse a large amount of data to save, so how to save memory is very important, or import a 100M file, the use of 1G of memory, the customer is simply crazy.

So the little ape took the first step to troubleshoot memory usage.

The first tool to use is the JDK's own, Jconsole.exe.

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

At first, the little ape found that its own program heap use of memory is not so big ah, how all add up and Task Manager inconsistent! Again, the Eden Space footprint of the program is small. The original is not set Eden space parameters, this to the boot configuration file to set:-xmn,-xx:newsize,-xx:maxnewsize,-xx:newratio These items can be set according to their own needs.

Because if the variable is not forced directly in the old Gen, the variables are first applied to the Eden Gen, then after the GC, if the variable survives, it enters the survivor area, and then after several GC, the variable exists in the old Gen area.

In fact, after the program started stable, most of the variables may have gone to the old Gen area, if your Eden area memory allocation is too large, the total minus survivor minus Eden after the rest of the older area will not be enough, this time the virtual machine what to do, Virtual opportunity automatically according to your set of-XMS and-xmx to expand, so you actually the virtual machine to get a lot of system memory is free memory, resulting in the task Manager to see more memory than you actually use much larger.

So the little ape was at the very first step. Start parameters, memory fruit Jane down some, but in fact, this is the illusion that the actual memory occupied in the virtual machine is still unchanged.

Little ape want to know more detailed memory use, this need to memoryanalyzer this tool, but before using this tool, you have to generate the program dump file, God I unexpectedly Baidu out of the method has to use to increase the start parameter + Heapdumponoutofmemoryerror and manually increase the exception outofmemory to generate the dump file.

Okay, the little ape came out, the machine down, good-hearted stuffed.

Finally found a good method, in fact, the JDK comes with the jconsole can be generated.

In the mbean->com.sun.management->hotspotdiagnostic-> operation->dumpheap, change the value of P0 to the path where you generate the dump file, and click Dumpheap. However, the suffix of the dump file is not dump!!! It's HPROF!!!

After the build, open with Memoryanalyzer, leak suspects, you will see a magical pie chart. It shows which instance occupy how much memory, there is a details, point.

Words can not open memoryanalyze child paper you jdk Ann, the Java environment variables are well-equipped.

See detailed memory footprint information, how many objects are there, and so on.

The little ape has finally found a place to optimize.

Before that, the Java string is resent, this is a deeper resentment, is the previous sentence, can use basic types of basic types.

The little ape found that there were 100,000 strings of object and 100,000 char[] objects.

Well, you save a string with string, in fact it uses the char inside him to save the string, and then he brings all his own relatives.

You save a "0", string gives you the size of the object is much larger than 1Byte!

So you can transfer string ToCharArray () method to char[] save.

Here's 818 when to use string, when to use StringBuffer, and when to use char.

In fact, it is excellent to define a commonly used constant string using string.

Like what

String strtemp = "01";

String STRTEMP2 = "01";

With this definition, strtemp and STRTEMP2 actually share an object, except that the reference to this object is 2!

So defining 10 billion is just a string object, which is a constant pool unique to string.

Then why do you use StringBuffer and Stringbuild?

Because StringBuffer and Stringbuild's append is better than string +!

And the string itself is designed by constant design, and StringBuffer and Stringbuild are really the strings that can be changed.

However, if the program is to save a large number of non-regular strings, this time it is recommended to convert to char to save.

This is only a string type, and other int is the same principle, try to use basic variables to save.

Looking at the tall Java, claiming that there is no memory leak of Java, if we use improper, it will cause memory waste.

While the memory will be released correctly at the moment of exiting the program, there are times when we are more concerned with running memory usage.

As long as the reference count of a variable is not 0,GC can not be recycled him, perhaps you this object temporarily useless, but add him to the end of the program to be released in the ArrayList, that this piece inside the operation is wasted.

Java is not a memory leak, but the time to release is also very important, an object for us is actually useless, but was accidentally the object's key to lend a life cycle longer than his object, for us is a memory leak.

Can take a good look at memoryanalyze, analysis of the existing objects, is not really useful, if there is useless, must be where the reference.

The little ape finally finished the first Java essay. Java Express One months later, next month will be moved to HTML5, do not read ............ ......

Java Memory optimization Kind

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.