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 throwsOutOfMemoryError
Or 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:gc
Or-Xloggc
When 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-inhprof
The tool can also complete this work. To usehprof
And keep it track of memory usage.-Xrunhprof:heap=sites
Option to call JVM.
Listing 3 shows the memory used by the application.hprof
Output. (hprof
When the application exitskill -3
Or press Ctrl + break in Windows to generate a break .) Note that compared to the two snapshots,Map.Entry
,Task
Andint[]
The object has increased significantly.
See listing 3.
Listing 4 showshprof
The other part of the output isMap.Entry
Call Stack information of the object's distribution point. This output tells us which call chains are generatedMap.Entry
Object 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.