On Android you use the Davilk virtual machine which is similar to the Java Virtual machine but it uses register-based Java stack-based Davilk virtual machine and the compiled files are different because it has a dex file and it has multiple classes whereas the Java virtual machine does Each file corresponds to a class

Davilk virtual machine is compiled into a class file and then packaged into a dex file using the DX tool.

An Android app is a Davilk virtual machine copied from the Davilk virtual machine in the Zygote process.

Adb shell cat /system/build.prop Displays the maximum Java Object heap size set by the system

Davilk virtual machine Java Object Heap size Navtive Heap size Java object heap size is limited by root ADB shell cat /system/build.prop to view the navtive heap size probability is unlimited

Adb shell cat /proc/pid/oom_adj Displays the current app process

Android foreground process 0 Visible process Background process 11 Service 5 Service 8 Empty process -11–16

If both processes are background processes 11, the one that occupies the most memory is reclaimed. So memory reclamation is very important

Adb shell cat /proc/pid/oom_adj

OOM issues:

Memory leak: A memory leak occurs when objects that are no longer used during the application cycle are referenced by GC ROOTS and cannot be recycled, reducing the actual available Java heap memory

Memory leakage exceeds the maximum memory usage of a single application, exceeding this value will result in OOM

The OOM has a lot of memory that does not necessarily exceed the maximum heap usage

OOM

1. JAVA heap memory overflow 2. Insufficient contiguous space 3. The number of threads exceeds the limit. 5. The virtual memory is insufficientCopy the code

We can use adb shell dumpsys meminfo –package package name

If multiple devices adb -s XXX shell

After downloading Hprof, you need to convert it into the file required by MAT through the Platm-tools hprof-conv.exe in Android SDK

Out-of-coming is who is referenced in the object and who is referenced in the in-comeing object

  1. Resource leaks such as in the database
  2. Registered object is not deregistered such as broadcast eventBus
  3. Class to hold large data objects.
  4. Leaks caused by singleton patterns such as the Context passing in an Activity
  5. A static instance of a non-static inner class
  6. Zero time leakage of Handler
  7. Objects in the container are not clear and null is set
  8. Webviews need to communicate using the AIDL process

The inJustDecodeBounds=true parameter to bitmapFactory. Option will not load the image and allocate memory, but will return the width and height of the image. Then use samleSize to scale the image

A pixel has one parameter: Config ALPHA(1) : 1 byte RGB_565:2 bytes ARGB_888:4 bytes ARGB_F16:8 bytes

The amount of memory used in Android images is that much

1920x1080x4x((device density/folder density)^2)/1024= KB

mdpi hdpi xdpi xxdpi xxxdpi 160 240 320 480 560

But images loaded on files and networks do not have 1920x1080x4/1024= KB

8 bits =1Byte

1024 byte (bytes) = 1 KB

1024KB=1MB 

1024MB=1GB 

1024GB=1TB

Android 3.0-4.4 Bitmap objects are stored in Java Heap pixels data is stored in Native Heap Android 5.0-7.0 Bitmap objects are stored in Java Heap pixels data is guaranteed in Java Heap Android 8.0 Bimap objects are stored in Java Heap pixels and data is stored in native Heap

The overall idea of memory optimization:

  1. Equipment classification:
  2. BitMap optimization unified image library online and offline monitoring
  3. Glide picture loading library

LeakCanary Memory detection tool has two parts: 1. Find out who is suspected of leaking. Principle 2. Diagnostics: The Haha open source library takes advantage of this accessibility analysis

LeakCanary principle The bindAppcalition method contentProvider.oncreate in ActivityThread will execute earlier than AppCaliton.oncreate

LeakCanary detection principle of Activity exit ActivityLifecycleCallbacks fragments can be monitored Activity life cycle four references, and the life cycle of JAVA reference queue ReferenceQueue relationshipCopy the code

The RefereQueue reference queue is the queue that holds the reference

The Reference object is added to the end of the Reference queue when the referenced object is reclaimed by the GC

When WeakReference WeakReference =new WeakReference(obj,referenceQueue)

Leakcanary Watch Adds an object 5s to the watch list