JNI Memory leakage

Source: Internet
Author: User
Http://www.blogjava.net/neumqp/archive/2006/03/02/33152.html
If the new object in C ++ does not return Java, it must be release; otherwise, the memory is leaked. Including newstringutf and newobject
. If the returned Java data does not need to be release, Java recycles it by itself.

Jstring jstr = env-> newstringutf (* P). sess_id );
...
Env-> deletelocalref (jstr );

Jobject jobj = env-> newobject (clazz, midinit );
Return jobj;

For memory leakage, you can see the increasing trend of memory as the program runs in Windows Resource Manager.HP jmeterDetection.Add JVM parameters when running the program-Xrunhprof: heap = All, cutoff = 0Generate java.hprof.txt and open it with jmeter,Metric-> residual objects (count), You can see the unrecycled object, select the object to view, clickMarkRecord the object to be viewed,Window-> New windowOpen a new window and useMetric-> reference graph tree,Then clickFind immediatelyYou can see where the object is referenced.

Find out another method for Memory leakage

The first sign of a program memory leak is that it throwsOutOfMemoryErrorOr the performance is poor due to frequent garbage collection. Fortunately, garbage collection can provide a large amount of information that can be used to diagnose memory leaks. If-verbose:gcOr-XloggcWhen JVM is called, a diagnostic information is printed on the console or log file each time the GC is run, including the time it takes, the current heap usage, and the memory size it has recovered. GC usage is not recorded. Therefore, if you need to analyze memory problems or tune the Garbage Collector, it is worthwhile to enable GC logs in the production environment by default.

Some tools can output GC logs and display them graphically. jtune is such a tool (see references ). Observe the heap size chart after GC to see the trend of program memory usage. For most programs, memory usage can be divided into two parts:BaselineUse andCurrent Load. For server applications, baseline usage means the memory usage when the application has no load but is ready to accept the request, current load is used in the process of processing the request, but the memory will be released after the request is processed. As long as the load is basically constant, applications usually quickly reach a stable memory usage level. If the memory usage continues to increase when the application Initialization is complete and the load is not increased, the program may retain the generated object when processing the previous request.

Figure 1 shows the change of application heap size over time after GC. The increasing trend is the warning signal of Memory leakage. (In real applications, the slope is not that big, but after collecting GC data for a long enough time, the upward trend is usually very obvious .)

Figure 1. Increasing memory usage Trend

After you are sure that there is a memory leak, the next step is to find out which object causes this problem. All memory analyzers can generate heap snapshots decomposed by object classes. There are some good commercial heap analysis tools, but finding out memory leaks doesn't have to spend money on these tools-built-inhprofThe tool can also complete this work. To usehprofAnd keep it track of memory usage.-Xrunhprof:heap=sitesOption to call JVM.

Listing 3 shows the memory used by the application.hprofOutput. (hprofWhen the application exitskill -3Or press Ctrl + break in Windows to generate a break .) Note that compared to the two snapshots,Map.Entry,TaskAndint[]The object has increased significantly.

See listing 3.

Listing 4 showshprofThe other part of the output isMap.EntryCall Stack information of the object's distribution point. This output tells us which call chains are generatedMap.EntryObject with some program analysis to find out the memory leak source is generally quite easy.

Listing 4. hprof output, showing the allocation point of the map. Entry object

            TRACE 300446:            java.util.HashMap$Entry.<init>(<Unknown Source>:Unknown line)            java.util.HashMap.addEntry(<Unknown Source>:Unknown line)            java.util.HashMap.put(<Unknown Source>:Unknown line)            java.util.Collections$SynchronizedMap.put(<Unknown Source>:Unknown line)            com.quiotix.dummy.MapLeaker.newTask(MapLeaker.java:48)            com.quiotix.dummy.MapLeaker.main(MapLeaker.java:64)            

In addition
Jstring jstr = (jstring) ENV-> callobjectmethod (authenrequest, mid_authenreq_getsdid_s );
Env-> getstringutfregion (jstr, 0, env-> getstringlength (jstr), authenreq. sd_id );
When jstr is null, env-> getstringlength (jstr) will fail, causing JVM crash.

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.