-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
46 changed files
with
4,263 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
plugins { | ||
id 'com.android.application' | ||
} | ||
|
||
android { | ||
compileSdkVersion 30 | ||
buildToolsVersion "30.0.3" | ||
|
||
defaultConfig { | ||
applicationId "com.baiiu.nativeleak" | ||
minSdkVersion 16 | ||
targetSdkVersion 30 | ||
versionCode 1 | ||
versionName "1.0" | ||
|
||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | ||
} | ||
|
||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
compileOptions { | ||
sourceCompatibility JavaVersion.VERSION_1_8 | ||
targetCompatibility JavaVersion.VERSION_1_8 | ||
} | ||
externalNativeBuild { | ||
cmake { | ||
path 'src/main/cpp/CMakeLists.txt' | ||
version "3.6.0" | ||
} | ||
} | ||
} | ||
|
||
dependencies { | ||
|
||
implementation 'androidx.appcompat:appcompat:1.2.0' | ||
implementation 'com.google.android.material:material:1.3.0' | ||
implementation 'androidx.constraintlayout:constraintlayout:2.0.4' | ||
testImplementation 'junit:junit:4.+' | ||
androidTestImplementation 'androidx.test.ext:junit:1.1.2' | ||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
26 changes: 26 additions & 0 deletions
26
jni/nativeleak/src/androidTest/java/com/baiiu/nativeleak/ExampleInstrumentedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.baiiu.nativeleak; | ||
|
||
import android.content.Context; | ||
|
||
import androidx.test.platform.app.InstrumentationRegistry; | ||
import androidx.test.ext.junit.runners.AndroidJUnit4; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* Instrumented test, which will execute on an Android device. | ||
* | ||
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a> | ||
*/ | ||
@RunWith(AndroidJUnit4.class) | ||
public class ExampleInstrumentedTest { | ||
@Test | ||
public void useAppContext() { | ||
// Context of the app under test. | ||
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); | ||
assertEquals("com.baiiu.nativeleak", appContext.getPackageName()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.baiiu.nativeleak"> | ||
|
||
<application | ||
android:allowBackup="true" | ||
android:icon="@mipmap/ic_launcher" | ||
android:label="@string/app_name" | ||
android:roundIcon="@mipmap/ic_launcher_round" | ||
android:supportsRtl="true" | ||
android:theme="@style/Theme.AndroidModule"> | ||
<activity android:name=".MainActivity"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
|
||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
</activity> | ||
</application> | ||
|
||
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# For more information about using CMake with Android Studio, read the | ||
# documentation: https://d.android.com/studio/projects/add-native-code.html | ||
|
||
# Sets the minimum version of CMake required to build the native library. | ||
|
||
cmake_minimum_required(VERSION 3.4.1) | ||
|
||
# 添加动态库 | ||
add_library( | ||
test-lib | ||
|
||
SHARED | ||
|
||
testLeak.cpp | ||
) | ||
|
||
# 从某路径下找到动态库并命名 | ||
find_library( | ||
log-lib | ||
log) | ||
|
||
# 关联动态库 | ||
target_link_libraries( | ||
test-lib | ||
${log-lib}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// | ||
// Created by baiiu on 2021/5/20. | ||
// | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// | ||
// Created by baiiu on 2021/5/20. | ||
// | ||
|
||
#include <android/log.h> | ||
#include <malloc.h> | ||
#include "jni.h" | ||
#include "string.h" | ||
|
||
/* | ||
* | ||
* | ||
这段代码: | ||
char *p = (char *) malloc(sizeof(char) * 1024 * 1024); | ||
__android_log_print(ANDROID_LOG_DEBUG, "mLogU", "%p has leaked 1M", p); | ||
每次分配1M,当虚拟内存分配完后,返回指针为null; | ||
点Home键后,直接崩溃了; | ||
adb shell cat proc/27581/statm | ||
363265 25825 17479 5 0 38044 0 | ||
624514 26183 17810 5 0 299292 0 | ||
886658 26352 17953 5 0 561436 0 | ||
1044866 26447 18021 5 0 719644 0 | ||
1044866 26457 18037 5 0 719644 0 | ||
1044866 26460 18037 5 0 719644 0 | ||
通过proc/[pid]/statm查看,当虚拟内存全部分配后,第一列vmSize已经不再变化,因为没有引用该值,索引 | ||
05-20 17:16:54.259 27435 27435 E AndroidRuntime: FATAL EXCEPTION: main | ||
05-20 17:16:54.259 27435 27435 E AndroidRuntime: Process: com.baiiu.nativeleak, PID: 27435 | ||
05-20 17:16:54.259 27435 27435 E AndroidRuntime: java.lang.OutOfMemoryError: pthread_create (1040KB stack) failed: Try again | ||
05-20 17:16:54.259 27435 27435 E AndroidRuntime: at java.lang.Thread.nativeCreate(Native Method) | ||
05-20 17:16:54.259 27435 27435 E AndroidRuntime: at java.lang.Thread.start(Thread.java:733) | ||
05-20 17:16:54.259 27435 27435 E AndroidRuntime: at android.app.QueuedWork.getHandler(QueuedWork.java:104) | ||
05-20 17:16:54.259 27435 27435 E AndroidRuntime: at android.app.QueuedWork.waitToFinish(QueuedWork.java:153) | ||
05-20 17:16:54.259 27435 27435 E AndroidRuntime: at android.app.ActivityThread.handleStopActivity(ActivityThread.java:4207) | ||
05-20 17:16:54.259 27435 27435 E AndroidRuntime: at android.app.servertransaction.StopActivityItem.execute(StopActivityItem.java:41) | ||
05-20 17:16:54.259 27435 27435 E AndroidRuntime: at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:145) | ||
05-20 17:16:54.259 27435 27435 E AndroidRuntime: at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70) | ||
05-20 17:16:54.259 27435 27435 E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) | ||
05-20 17:16:54.259 27435 27435 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:106) | ||
05-20 17:16:54.259 27435 27435 E AndroidRuntime: at android.os.Looper.loop(Looper.java:193) | ||
05-20 17:16:54.259 27435 27435 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6669) | ||
05-20 17:16:54.259 27435 27435 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) | ||
05-20 17:16:54.259 27435 27435 E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) | ||
05-20 17:16:54.259 27435 27435 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) | ||
char *p = (char *) malloc(sizeof(char) * 1024 * 1024); | ||
char *s = "hello_world"; | ||
strcpy(p, s); | ||
__android_log_print(ANDROID_LOG_DEBUG, "mLogU", "%p has leaked 1M", p); | ||
每次分配1M,当虚拟内存分配完后,返回指针为null;为null时调用strcpy因空指针崩溃 | ||
当修复空指针问题后,按Home键崩溃时候日志和第一种一样 | ||
generic_x86_arm:/ $ cat proc/27850/statm | ||
363265 25782 17421 5 0 38044 0 | ||
624514 27164 17735 5 0 299292 0 | ||
886786 28203 17741 5 0 561564 0 | ||
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' | ||
05-20 17:46:57.373 27945 27945 F DEBUG : Revision: '0' | ||
05-20 17:46:57.373 27945 27945 F DEBUG : ABI: 'x86' | ||
05-20 17:46:57.373 27945 27945 F DEBUG : pid: 27850, tid: 27850, name: aiiu.nativeleak >>> com.baiiu.nativeleak <<< | ||
05-20 17:46:57.373 27945 27945 F DEBUG : signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x0 | ||
05-20 17:46:57.373 27945 27945 F DEBUG : Cause: null pointer dereference | ||
05-20 17:46:57.373 27945 27945 F DEBUG : eax 00000000 ebx d9211fdc ecx f349f2a0 edx 00646c72 | ||
05-20 17:46:57.373 27945 27945 F DEBUG : edi 00000000 esi d9210652 | ||
05-20 17:46:57.373 27945 27945 F DEBUG : ebp ff7fc328 esp ff7fc2c4 eip f349f2a7 | ||
05-20 17:46:57.479 27945 27945 F DEBUG : | ||
05-20 17:46:57.479 27945 27945 F DEBUG : backtrace: | ||
05-20 17:46:57.479 27945 27945 F DEBUG : #00 pc 0001e2a7 /system/lib/libc.so (strcpy+1591) | ||
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) | ||
由此可见虚拟内存使用完了造成的影响很大,无法mallcoc,正常业务无法运行; | ||
首先应当尽量避免内存泄露,然后看看能否使用64位so,增大虚拟内存寻址空间; | ||
*/ | ||
|
||
extern "C" | ||
JNIEXPORT void JNICALL | ||
Java_com_baiiu_nativeleak_MainActivity_testLeak(JNIEnv *env, jobject thiz) { | ||
char *p = (char *) malloc(sizeof(char) * 1024 * 1024); | ||
char *s = "hello_world"; | ||
if (p) { | ||
strcpy(p, s); | ||
} | ||
__android_log_print(ANDROID_LOG_DEBUG, "mLogU", "%p has leaked 1M", p); | ||
} |
Oops, something went wrong.