Skip to content

Commit 491bb3e

Browse files
committed
test native leak
1 parent a34c8f0 commit 491bb3e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+4263
-0
lines changed

jni/nativeleak/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

jni/nativeleak/build.gradle

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
plugins {
2+
id 'com.android.application'
3+
}
4+
5+
android {
6+
compileSdkVersion 30
7+
buildToolsVersion "30.0.3"
8+
9+
defaultConfig {
10+
applicationId "com.baiiu.nativeleak"
11+
minSdkVersion 16
12+
targetSdkVersion 30
13+
versionCode 1
14+
versionName "1.0"
15+
16+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
17+
}
18+
19+
buildTypes {
20+
release {
21+
minifyEnabled false
22+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
23+
}
24+
}
25+
compileOptions {
26+
sourceCompatibility JavaVersion.VERSION_1_8
27+
targetCompatibility JavaVersion.VERSION_1_8
28+
}
29+
externalNativeBuild {
30+
cmake {
31+
path 'src/main/cpp/CMakeLists.txt'
32+
version "3.6.0"
33+
}
34+
}
35+
}
36+
37+
dependencies {
38+
39+
implementation 'androidx.appcompat:appcompat:1.2.0'
40+
implementation 'com.google.android.material:material:1.3.0'
41+
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
42+
testImplementation 'junit:junit:4.+'
43+
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
44+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
45+
}

jni/nativeleak/proguard-rules.pro

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baiiu.nativeleak;
2+
3+
import android.content.Context;
4+
5+
import androidx.test.platform.app.InstrumentationRegistry;
6+
import androidx.test.ext.junit.runners.AndroidJUnit4;
7+
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
11+
import static org.junit.Assert.*;
12+
13+
/**
14+
* Instrumented test, which will execute on an Android device.
15+
*
16+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
17+
*/
18+
@RunWith(AndroidJUnit4.class)
19+
public class ExampleInstrumentedTest {
20+
@Test
21+
public void useAppContext() {
22+
// Context of the app under test.
23+
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
24+
assertEquals("com.baiiu.nativeleak", appContext.getPackageName());
25+
}
26+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.baiiu.nativeleak">
4+
5+
<application
6+
android:allowBackup="true"
7+
android:icon="@mipmap/ic_launcher"
8+
android:label="@string/app_name"
9+
android:roundIcon="@mipmap/ic_launcher_round"
10+
android:supportsRtl="true"
11+
android:theme="@style/Theme.AndroidModule">
12+
<activity android:name=".MainActivity">
13+
<intent-filter>
14+
<action android:name="android.intent.action.MAIN" />
15+
16+
<category android:name="android.intent.category.LAUNCHER" />
17+
</intent-filter>
18+
</activity>
19+
</application>
20+
21+
</manifest>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# For more information about using CMake with Android Studio, read the
2+
# documentation: https://d.android.com/studio/projects/add-native-code.html
3+
4+
# Sets the minimum version of CMake required to build the native library.
5+
6+
cmake_minimum_required(VERSION 3.4.1)
7+
8+
# 添加动态库
9+
add_library(
10+
test-lib
11+
12+
SHARED
13+
14+
testLeak.cpp
15+
)
16+
17+
# 从某路径下找到动态库并命名
18+
find_library(
19+
log-lib
20+
log)
21+
22+
# 关联动态库
23+
target_link_libraries(
24+
test-lib
25+
${log-lib})
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
//
2+
// Created by baiiu on 2021/5/20.
3+
//
4+
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
//
2+
// Created by baiiu on 2021/5/20.
3+
//
4+
5+
#include <android/log.h>
6+
#include <malloc.h>
7+
#include "jni.h"
8+
#include "string.h"
9+
10+
/*
11+
*
12+
*
13+
这段代码:
14+
char *p = (char *) malloc(sizeof(char) * 1024 * 1024);
15+
__android_log_print(ANDROID_LOG_DEBUG, "mLogU", "%p has leaked 1M", p);
16+
每次分配1M,当虚拟内存分配完后,返回指针为null;
17+
点Home键后,直接崩溃了;
18+
19+
adb shell cat proc/27581/statm
20+
363265 25825 17479 5 0 38044 0
21+
624514 26183 17810 5 0 299292 0
22+
886658 26352 17953 5 0 561436 0
23+
1044866 26447 18021 5 0 719644 0
24+
1044866 26457 18037 5 0 719644 0
25+
1044866 26460 18037 5 0 719644 0
26+
通过proc/[pid]/statm查看,当虚拟内存全部分配后,第一列vmSize已经不再变化,因为没有引用该值,索引
27+
28+
29+
05-20 17:16:54.259 27435 27435 E AndroidRuntime: FATAL EXCEPTION: main
30+
05-20 17:16:54.259 27435 27435 E AndroidRuntime: Process: com.baiiu.nativeleak, PID: 27435
31+
05-20 17:16:54.259 27435 27435 E AndroidRuntime: java.lang.OutOfMemoryError: pthread_create (1040KB stack) failed: Try again
32+
05-20 17:16:54.259 27435 27435 E AndroidRuntime: at java.lang.Thread.nativeCreate(Native Method)
33+
05-20 17:16:54.259 27435 27435 E AndroidRuntime: at java.lang.Thread.start(Thread.java:733)
34+
05-20 17:16:54.259 27435 27435 E AndroidRuntime: at android.app.QueuedWork.getHandler(QueuedWork.java:104)
35+
05-20 17:16:54.259 27435 27435 E AndroidRuntime: at android.app.QueuedWork.waitToFinish(QueuedWork.java:153)
36+
05-20 17:16:54.259 27435 27435 E AndroidRuntime: at android.app.ActivityThread.handleStopActivity(ActivityThread.java:4207)
37+
05-20 17:16:54.259 27435 27435 E AndroidRuntime: at android.app.servertransaction.StopActivityItem.execute(StopActivityItem.java:41)
38+
05-20 17:16:54.259 27435 27435 E AndroidRuntime: at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:145)
39+
05-20 17:16:54.259 27435 27435 E AndroidRuntime: at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70)
40+
05-20 17:16:54.259 27435 27435 E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
41+
05-20 17:16:54.259 27435 27435 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:106)
42+
05-20 17:16:54.259 27435 27435 E AndroidRuntime: at android.os.Looper.loop(Looper.java:193)
43+
05-20 17:16:54.259 27435 27435 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6669)
44+
05-20 17:16:54.259 27435 27435 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
45+
05-20 17:16:54.259 27435 27435 E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
46+
05-20 17:16:54.259 27435 27435 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
47+
48+
49+
50+
char *p = (char *) malloc(sizeof(char) * 1024 * 1024);
51+
char *s = "hello_world";
52+
strcpy(p, s);
53+
__android_log_print(ANDROID_LOG_DEBUG, "mLogU", "%p has leaked 1M", p);
54+
每次分配1M,当虚拟内存分配完后,返回指针为null;为null时调用strcpy因空指针崩溃
55+
当修复空指针问题后,按Home键崩溃时候日志和第一种一样
56+
57+
generic_x86_arm:/ $ cat proc/27850/statm
58+
363265 25782 17421 5 0 38044 0
59+
624514 27164 17735 5 0 299292 0
60+
886786 28203 17741 5 0 561564 0
61+
62+
63+
05-20 17:46:57.373 27945 27945 F DEBUG : Build fingerprint: 'google/sdk_gphone_x86_arm/generic_x86_arm:9/PSR1.180720.117/5875966:user/release-keys'
64+
05-20 17:46:57.373 27945 27945 F DEBUG : Revision: '0'
65+
05-20 17:46:57.373 27945 27945 F DEBUG : ABI: 'x86'
66+
05-20 17:46:57.373 27945 27945 F DEBUG : pid: 27850, tid: 27850, name: aiiu.nativeleak >>> com.baiiu.nativeleak <<<
67+
05-20 17:46:57.373 27945 27945 F DEBUG : signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x0
68+
05-20 17:46:57.373 27945 27945 F DEBUG : Cause: null pointer dereference
69+
05-20 17:46:57.373 27945 27945 F DEBUG : eax 00000000 ebx d9211fdc ecx f349f2a0 edx 00646c72
70+
05-20 17:46:57.373 27945 27945 F DEBUG : edi 00000000 esi d9210652
71+
05-20 17:46:57.373 27945 27945 F DEBUG : ebp ff7fc328 esp ff7fc2c4 eip f349f2a7
72+
05-20 17:46:57.479 27945 27945 F DEBUG :
73+
05-20 17:46:57.479 27945 27945 F DEBUG : backtrace:
74+
05-20 17:46:57.479 27945 27945 F DEBUG : #00 pc 0001e2a7 /system/lib/libc.so (strcpy+1591)
75+
05-20 17:46:57.479 27945 27945 F DEBUG : #01 pc 00000619 /data/app/com.baiiu.nativeleak-wL45YswCp5R9mJtUY58XoQ==/lib/x86/libnative-lib.so (Java_com_baiiu_nativeleak_MainActivity_testLeak+137)
76+
77+
由此可见虚拟内存使用完了造成的影响很大,无法mallcoc,正常业务无法运行;
78+
首先应当尽量避免内存泄露,然后看看能否使用64位so,增大虚拟内存寻址空间;
79+
*/
80+
81+
extern "C"
82+
JNIEXPORT void JNICALL
83+
Java_com_baiiu_nativeleak_MainActivity_testLeak(JNIEnv *env, jobject thiz) {
84+
char *p = (char *) malloc(sizeof(char) * 1024 * 1024);
85+
char *s = "hello_world";
86+
if (p) {
87+
strcpy(p, s);
88+
}
89+
__android_log_print(ANDROID_LOG_DEBUG, "mLogU", "%p has leaked 1M", p);
90+
}

0 commit comments

Comments
 (0)