Explore memory optimization in-depth Java virtual machines

Source: Internet
Author: User

In the previous article, we talked about the architecture and memory model of the Java Virtual machine, so we had to talk about memory leaks. As you know, Java is developed from C + +, and a big problem with C + + programs is that memory leaks are hard to solve, although Java's JVM has its own garbage collection mechanism to reclaim memory, and in most cases it does not require Java program developers to have too much of a mind, But there is a leak problem, just a little smaller than C + +. For example, there is a referenced but useless object in the program: The program refers to the object, but subsequent no or no longer uses it, the memory space it consumes is wasted.

Let's take a look at how the GC works: monitor the running state of each object, including the application, reference, reference, assignment, etc. of the object, and when the object is no longer referenced, dispose of the object (GC focuses on this article without much elaboration). Many Java programmers rely too much on GC, but the point is that regardless of how well the JVM's garbage collection works, memory is always a limited resource, so even if the GC does most of the garbage collection for us, it is necessary to pay due attention to memory optimization in the encoding process. This can effectively reduce the number of GC times, while improving memory utilization, maximize the efficiency of the program.

Overall, the memory optimization of a Java Virtual machine should begin in two ways: Java virtual machines and Java applications. The former refers to the size of the virtual machine logical memory partition according to the design of the application through the virtual machine parameters, so that the memory of the virtual machine and the program to the memory needs to match; the latter refers to the optimizer algorithm, which reduces the GC burden and improves the GC recovery success rate.

The parameters to optimize the virtual machine memory parameters are as follows:
-xms
Initial Heap Size

-xmx
Java Heap Maximum Value

-xmn
The heap size of young generation

-xss
Stack size per thread

These are the three more commonly used parameters, and others:
-xx:minheapfreeratio=40
Minimum percentage of heap free after GC to avoid expansion.

-xx:maxheapfreeratio=70
Maximum percentage of heap free after GC to avoid shrinking.

-xx:newratio=2
Ratio of new/old generation sizes. [Sparc-client:8; x86-server:8; x86-client:12.] -client:8 (1.3.1+), X86:12]

-xx:newsize=2.125m
Default size of new generation (in bytes) [5.0 and newer:64 bit VMs is scaled 30% larger; x86:1m; x86, 5.0 and OLDER:64 0K]

-xx:maxnewsize=
Maximum size of new generation (in bytes). Since 1.4, Maxnewsize is computed as a function of newratio.

-xx:survivorratio=25
Ratio of Eden/survivor space size [Solaris amd64:6; Sparc in 1.3.1:25; Other Solaris platforms in 5.0 and EARLIER:32]

-xx:permsize=
Initial size of permanent generation

-xx:maxpermsize=64m
Size of the Permanent Generation. [5.0 and newer:64 bit VMs are scaled 30% larger; 1.4 amd64:96m; 1.3.1-client:32m.]


The following is said by optimizing the program algorithm to improve memory utilization, and reduce memory risk, completely is the experience of experience, for reference only, thank you!


1. Release the reference to the useless object as soon as possible (XX = null;)

Look at the code:

Public list<pagedata> Parse (HtmlPage page) {

list<pagedata> list = null;

try {

List valueList = Page.getbyxpath (Config.getcontentxpath ());

if (valueList = = NULL | | valuelist.isempty ()) {

return list;

}

Create objects when needed, save memory and increase efficiency

List = new arraylist<pagedata> ();

Pagedata pagedata = new Pagedata ();

StringBuilder value = new StringBuilder ();

for (int i = 0; i < valuelist.size (); i++) {

HtmlElement content = (htmlelement) valuelist.get (i);

Domnodelist

if (IMGs! = null &&!imgs.isempty ()) {

for (HtmlElement Img:imgs) {

try {

HtmlImage image = (htmlimage) img;

String path = Image.getsrcattribute ();

String format = path.substring (Path.lastindexof ("."), Path.length ());

String LocalPath = "d:/images/" + md5helper.md5 (path). replace ("\ \", ","). Replace ("/", ",") + format;

File LocalFile = new file (LocalPath);

if (!localfile.exists ()) {

Localfile.createnewfile ();

Image.saveas (LocalFile);

}

Image.setattribute ("src", "file:///" + LocalPath);

LocalFile = null;

image = NULL;

img = NULL;

} catch (Exception e) {

}

}

This object will not be used in the future, clear the reference to it, equivalent to inform the GC in advance, the object can be recycled

IMGs = null;

}

String text = Content.asxml ();

Value.append (text). Append ("<br/>");

Valuelist=null;

content = null;

Text = null;

}

Pagedata.setcontent (Value.tostring ());

Pagedata.setcharset (Page.getpageencoding ());

List.add (Pagedata);

Here Pagedata=null; is useless because the list still holds a reference to the object, and the GC does not recycle it

Value=null;

There is no list=null here; Because list is the return value of a method, the return value you get from the method is always empty, and this error is not easily discovered, excluded

} catch (Exception e) {

}

return list;

}



PS: If you have any questions, please make it directly in group 457036818.

This article is from the Java Learning Video tutorial blog, so be sure to keep this source http://10239772.blog.51cto.com/10229772/1659199

Explore memory optimization in-depth Java virtual machines

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.