Current location - Health Preservation Learning Network - Healthy weight loss - How to optimize the running memory occupation of app?
How to optimize the running memory occupation of app?
The following methods can optimize the running memory of the application:

1. Release memory when memory resources are tight.

At any stage of the application life cycle? onTrimMemory()? The callback method can tell you that the memory of the device is getting lower and lower.

You can release resources according to the memory tension pushed by this method.

2. Use optimized data containers.

Use the optimized data container of Android framework, such as? Sparse array? SparseBooleanArray? And then what? LongSparseArray。

The traditional implementation of HashMap in memory is very inefficient because it needs to establish a Mapping relationship for each item in the map. SparseArray class is very efficient because it avoids the need for autobox keys in the system.

3. Use conservative services.

If your application needs to use service to perform business functions in the background, please don't let it run in the background all the time unless it has been doing active work (such as requesting data from the server every few seconds). Service? Be careful when the execution is completed but the stop fails? Service? Resulting in a memory leak.

4. Beware of abstract code

Generally speaking, it is a good programming habit to use simple abstraction, because abstraction can provide code scalability and maintainability to a certain extent. However, abstraction will bring significant overhead: more code needs to be executed, and it takes longer time and more running memory to map code to memory, so if abstraction does not bring significant effect, try to avoid it.

So how do you check how much memory the application is running?

How to check the running memory of mobile phone;

1. Some mobile phones have a built-in memory manager/smart manager. Open this application to check memory usage.

2. Some machines: press and hold the Home key-enter the task manager -RAM status-view.

Tip: Different models of mobile phones may have slightly different viewing paths.