- Dealing with Large Memory Requirements on Android
- First Some Theory
- Stack Memory
- Heap Memory
- Analyzing Heap Memory
- Memory Leaks
- Release Memory in Response to Events
- Large Heap
- Checking How Much Memory Your App Can Use
- Use Optimized Data Structures
- Prevent Memory Churn
- Optimizing PSPDFKit’s Memory Usage
- Conclusion
- How to clear Android internal storage from the files you don’t need anymore
- Written by: Ilia
- Error: not enough memory on your mobile device
- Figuring out how much free storage space is left on your Android
- How to clear phone memory: tips & tricks
- Remove barely used apps from Android system memory
- Transferring files onto a memory card
- Transferring not used apps from internal to external memory card
- Advice. Install FolderMount application
- Deleting app cache
- Delete unwanted files via Piriform CCleaner
- How to increase internal storage of Android phone by deleting unnecessary files
- Using Storage Analyzers
- Transferring photos and videos to Google Photos
- Memory clearing: Q&A
Dealing with Large Memory Requirements on Android
This article focuses on basic techniques for managing memory usage in applications — such as web browsers, photo editors, and PDF viewers — that have large memory requirements.
First Some Theory
Most apps on Android run on top of Android Runtime (ART), which replaced the now deprecated Dalvik virtual machine. ART and Dalvik are similar to Java Virtual Machine (JVM) in that they share its underlying design principles: They use two separate memory spaces to store application data — stack and heap.
Stack Memory
Java stack memory is used to store local variables (primitive types and references to objects). Each Java thread has its own separate stack. Stack memory is relatively small compared to heap memory. The Java stack size on Dalvik is usually 32 KB for Java code and 1 MB for native (C++/JNI) code. ART introduced a unified stack for both Java and C++ that is around 1 MB.
When an app hits the stack memory limit, StackOverflowError is emitted. The most probable cause for hitting the stack limit is either infinite recursion or an excessively deep method call. Stack memory is always referenced in a LIFO (last in, first out) fashion. Whenever a method call is made, a new block (stack frame) with the method’s local variables is pushed to the stack. When the method completes, its stack frame is popped from the stack and any possible result value is pushed back onto the stack. Infinite recursion or an excessively deep call hierarchy will lead to hitting the stack size limit. The former problem is a bug that can be easily fixed, but the latter needs some refactoring in the form of unwrapping recursive method calls into a cycle.
Heap Memory
Java heap memory is used by the virtual machine to allocate objects. Whenever you create an object, it’s always created in the heap. Virtual machines, like JVM or ART, perform regular garbage collection (GC), making the heap memory of all objects that are not referenced anymore available for future allocations.
To provide a smooth user experience, Android sets a hard limit on the heap size for each running application. The heap size limit varies among devices and is based on how much RAM a device has. If your app hits this heap limit and tries to allocate more memory, it will receive an OutOfMemoryError and will terminate. So let’s look at some of the techniques for preventing this situation.
Analyzing Heap Memory
The most important tool for investigating memory problems in your apps and for understanding how the memory is used is the Memory Profiler available in Android Studio.
This tool provides visualization for how much memory your app takes over time. You can take snapshots of the running app’s Java heap, record memory allocations, and inspect the heap or recorded memory allocations in a powerful UI.
Your typical memory profiler session should look like this:
Look for frequent memory allocations and garbage collector runs to detect possible performance issues.
Look for memory usage over time, especially after operations that are known to require a lot of memory allocation. Make sure that memory usage goes down after these operations are done. For example, below you can see the memory impact of PSPDFKit’s PdfActivity after loading a document.
Dump the heap at multiple points of your app runtime to inspect your memory usage. Look for large objects that are kept in memory and prevented from being garbage collected. Heap dumps can also help identify memory leaks — for example, you can search for your activities in the heap dump to see if the old instances have been collected.
Here at PSPDFKit, we use the memory profiler extensively to spot memory usage issues while working with complex documents. We also use it regularly to get a quick idea of how big of a memory hit we are introducing with our new features or various refactorings (we always balance memory usage and performance).
Memory Leaks
Today’s garbage collectors are complex pieces of state-of-the-art technology — the result of many years of research and development from countless people ranging from academics to development professionals. However, we still need to take care to avoid introducing memory leaks.
The industry standard for detecting memory leaks is the LeakCanary library. It automatically shows notifications when a memory leak is detected in your development builds, providing you with a stack trace of the leak in its UI. You can (and should) easily integrate it today!
Introducing new memory leaks is especially easy when working with complex lifecycles of Android activities or fragments. It’s a common occurrence in places where developers hold strong references to UI Context s or other UI-specific objects in a background task or in their static variables. One of the ways to trigger these leaks is to rotate your device multiple times while testing your app.
Release Memory in Response to Events
Android can reclaim memory of your app or outright kill your app when it’s necessary to free memory for more critical tasks. Before this happens, the system gives you a chance to release any memory you don’t need. You’ll need to implement the ComponentCallbacks2 interface in your activity. Then, whenever the system is under memory pressure, you’ll receive a call to the onTrimMemory() method and can release memory or disable features that won’t work in such low-memory situations.
PSPDFKit also handles these callbacks. We designed PSPDFKit to use a lot of memory for caching so we can make things more fluid. We don’t know how much memory is available on a device, so PSPDFKit will adapt and restrict usage when we get low-memory notifications. This makes it possible to run PSPDFKit-powered apps even on low-end devices, albeit with worse performance due to disabled caching.
Large Heap
One immediate solution for dealing with large memory requirements is to request a large Dalvik heap for your app. You can do this by adding android:largeHeap=»true» to your tag in AndroidManifest.xml
If the largeHeap property is set to true , Android will create all processes for your application with a large heap. This setting is only intended for apps that can’t work without it due to their nature, i.e. they are using large resources that need to fit into memory at the same time. It is strongly advised that you don’t use a large heap just to allow higher memory usage. You should always optimize your memory usage, because a large heap on low-memory, low-end devices can still be too small for your application.
Checking How Much Memory Your App Can Use
It’s always a good idea to check how big the heap of your app is and dynamically adapt your code and available features to these memory limits. You can check the maximum heap size at runtime by using the methods getMemoryClass() or getLargeMemoryClass() (when a large heap is enabled).
Android supports devices with as little as 512 MB of RAM. Make sure you remember low-end devices too! You can check whether your app is running on a low-memory device via the isLowRamDevice() method. The exact behavior of this method depends on the device, but it usually returns true on devices with less than 1 GB of RAM. You should make sure that your app works correctly on these devices and disable any features with large memory usage.
You can read more details about how Android behaves on low-memory devices, as well as some additional memory optimization tips for them, here.
Use Optimized Data Structures
In many cases, applications use too much memory simply because they use suboptimal data structures.
Java collections can’t store efficient primitive types and require boxing for their keys or values. For example, HashMap with integer keys should be replaced with the optimized SparseArray . Ultimately, you can always use raw arrays instead of collections, which is a great idea when your collection does not resize.
Other memory-inefficient data structures include various serializations. While it’s true that XML or JSON formats are easy to use, using a more efficient binary format such as protocol buffers will lead to lower memory usage.
These examples of memory-optimized data structures are meant only as a hint. As with all refactorings, you should first find the source of your problems before proceeding with these performance optimizations.
Prevent Memory Churn
Java/Android virtual machines allocate objects very quickly. Garbage collection is pretty fast too. However, allocating a large number of objects in a small time slice could lead to a problem called memory churn. In such a case, the virtual machine and garbage collector can’t sustain such frequent allocations and the application slows down or, in extreme cases, gets into an out-of-memory situation.
The main problem for us in Android land is that we have no control over when the GC is performed. This can potentially lead to performance problems — for example, if the GC runs during a UI animation and we miss the 16 ms threshold for smooth frame rendering. Thus it is important to prevent excessive allocations in our code.
One example of a situation that leads to memory churn is allocating large objects such as Paint inside the onDraw() method of a view. This creates many objects quickly and could lead to garbage collection that can negatively impact the performance of the view. You should always take care to monitor your memory usage to prevent these situations.
Optimizing PSPDFKit’s Memory Usage
Here at PSPDFKit, we’ve built an SDK that can deal with very complex documents. Since rendering PDFs is a memory-consuming task, we need to be extra careful in making sure apps using PSPDFKit won’t hit Android’s memory limits, which will result in the app being killed by the OS.
We have an extensive guide that lists most of the tricks described in this article in the context of PSPDFKit-powered apps. If you are integrating PSPDFKit, I strongly recommend you take a look at it.
Conclusion
Random-access memory (RAM) can be rather constrained on mobile devices. Making sure that you are using it efficiently is especially important while building apps that work with bigger objects such as large bitmaps (PDF viewers, web browsers, photo editors) or large media files (audio or video editors). Tips from this article are a must for every developer who wishes to build quality apps with acceptable behavior, even on low-memory devices.
I hope that you won’t see any memory-related crashes in your apps. Happy coding!
Источник
How to clear Android internal storage from the files you don’t need anymore
Written by: Ilia
Data recovery specialist, guest author, journalist.
There are two cases indicating that your phone lacks internal storage:
- Apps performance isn’t good enough and Android OS operation is slowed down;
- The phone displays a message saying that actions need to be taken in order to free up your storage space.
Phone internal memory has fixed storage volume and increasing it seems to be impossible. However, in this guide we’re going to tell you how to clear storage on your phone from the files and apps you don’t need anymore.
By freeing up space on Android memory, you’ll save time and your phone / tablet performance drawbacks won’t be bothering you anymore as well. The whole “cleaning” process will take just 20 minutes of your time if not less.
Error: not enough memory on your mobile device
Usually Android displays this message if a process or an application lack free space on your phone internal storage space.
Lack of free memory can expose itself by constantly freezing up during device performance. You probably won’t notice it right after purchasing a new phone, but over time with dozens of mobile apps being installed onto it and after accumulating a lot of “trash”, these freeze ups will become quite evident.
A following question arises: do the given technical characteristics of any phone «lie»? If not, then why do other owners of the same smartphone / tablet have no problems concerning this case?
Figuring out how much free storage space is left on your Android
When you receive a notification claiming that internal storage is insufficient, a following question arises: how much free storage space do you have and which part of it is occupied?
You can check the amount of free storage space via your mobile phone settings. In order to do this, open Settings – Device Maintenance – Storage – Storage settings – Device memory. Carefully study the data and pay attention to the following figures:
- Total space shows the amount of Android internal storage space
- System memory shows the minimum amount of space needed for system to properly operate
- Available space indicates how much space is left from the whole internal storage space.
Accordingly, if there isn’t enough built-in memory, you have to make the amount of free storage space equal to the volume of System memory, so that the system won’t show you the corresponding error.
In the following chapters, we’re going to tell you
how to clear space on Android phone.
How to clear phone memory: tips & tricks
You can delete what you don’t need anymore via operating system built-in tools and through third-party apps. They analyze the occupied space and help to identify files which can be safely deleted.
Remove barely used apps from Android system memory
We’re pretty sure that there are some apps installed on your phone which are barely used. Their size can be up to hundreds of megabytes (including cache).
You can uninstall useless apps via a standard App Manager (Settings – Apps) and free up space on Android phone by the following way.
On Android 8, it’s pretty convenient to use a free utility Files Go in order to detect not needed anymore apps. It’s available for download on other OS versions through Google Play.
How to uninstall not used apps via Files Go:
- Go to Unused apps section,
- Sort your apps by modification date or their size,
- Mark not needed programs and click Uninstall in order to delete them.
Transferring files onto a memory card
Phone internal storage, as it has already been mentioned, has fixed volume, so you have to constantly check whether there is enough free storage space for the correct apps and OS operation.
Generally, Android memory can be divided into two parts: internal and external. External memory can be easily increased, fortunately nowadays sd cards aren’t inexpensive at all (for $25 you can buy a memory card with the capacity of 256 GB).
We recommend you to transfer onto a memory card photos, videos, documents and other large files which you don’t have to necessarily store on internal memory. Moreover, it’s safer to store them on a memory card.
Actually, you can transfer files via any filemanager both through your phone or PC.
Transferring not used apps from internal to external memory card
You can transfer apps via Android application manager. The most convenient part of it is that you free up internal storage space without uninstalling barely used apps.
By transferring apps, you’ll save a couple of hundred megabytes of free space. Since the cache is also transferred alongside the app, the amount of space you save is significant.
Basically, with the introduction of Android Marshmallow version everything has become much easier. You simply have to format the memory card as internal storage via the settings. There is no need to do anything else, as Android OS will figure out which apps to store on the memory card on its own.
Advice. Install FolderMount application
FolderMount program expands the volume of your phone internal memory. It works also on Android 4.2.1 and above, while its counterparts don’t support a multiuser mode. Root is required for this app work.
Deleting app cache
In some cases app cache takes up an unbelievable amount of free space on your phone memory.
How to delete cache from internal storage:
- You can find out how much one or another app consumes via Settings – Apps section.
- In the list of installed apps click on the one that bothers you.
- See how much of the space is occupied via Storage section.
- To clear the cache, click on Clear Cache button.
Delete unwanted files via Piriform CCleaner
CCleaner will free up storage space on your phone and at the same time clear space on Android from temporary files and app cache, find large files and tell you how to optimize your device internal storage. The program is free and you can download it here. It’s very easy to use the above mentioned Files Go app as a counterpart to CCleaner.
How to increase internal storage of Android phone by deleting unnecessary files
Any file manager is suitable for manual clearing of your phone storage. To free up memory on Android, we recommend ES Explorer or Total Commander.
Be careful and delete from Android internal memory only unnecessary user files which you’ve created / copied yourself.
So, go to any file manager, open the root of internal memory and start searching for files and deleting the ones which are not used anymore.
The files that should be deleted (or transferred to a memory card) first of all:
- Photos, videos, recordings and other documents stored on internal memory instead of a sd;
- Documents received by email or via social networks (most often they are saved into Download folder);
- E-books and other files stored by third-party apps in your device memory;
- The contents of DCIM, bluetooth and sounds folders.
Using Storage Analyzers
In order for you to easily get how to do it, we recommend using Files Go app or any other storage analyzer for Android, which will show you in the form of a diagram the files occupying the most space as well as their location. Among those apps we’ll highlight:
Transferring photos and videos to Google Photos
Photos and videos in particular “eat” the most space on your phone. If your phone doesn’t support a memory card, transfer to the cloud the files you don’t need to have on you phone all the time. Photos or Google Photos apps suit these purposes the most. They automatically upload photos to the service, where they are available in their original quality via a browser or an app.
Apart from Google Photos, you can turn your attention to such alternative apps as Dropbox, Flickr or Microsoft OneDrive.
Even when the photos are only available on the server, you can easily get access to them if you have Internet connection. And most importantly, it’s actually a very convenient and fast way to free up memory on Android phone!
Memory clearing: Q&A
1. My phone lacked internal memory, I transferred half of the photos to a sd card, but after it has been done I opened them and they were all blurry. I tried to transfer them back to Android internal memory, but nothing has changed. How do I return the original photos, i.e. without any corruption?
2. I was in need to free up internal storage space on my phone, so I decided to clear it. I transferred a part of data (photos, music) to a memory card. Now the files can’t be read, even though the phone recognizes the card. How can I bring back the photos at least?
3. My phone is Samsung A5. I had no clue how to increase internal memory, so I transferred via my laptop folders with music and some files from the internal memory to a sd card. After that, when trying to open the folders, I found all of them empty. Neither my phone nor my computer can see the files and music. The amount of phone free internal memory didn’t seem to lessen after that. How can I find the files?
The answer. Probably, you’ve copied onto the memory card thumbnails instead of their originals. The original photos may have remained on phone internal memory. If it’s not the case, DiskDigger program will help you to cope with it.
If there isn’t enough space on your device memory, you should copy some files onto your computer (by creating a backup copy) and only after it’s done transfer the files to a memory card. You might find useful reading the guide on
how to clear internal storage on Android (see the text above).
My phone is Sony Xperia, when I open Play Market in order to download any program, it says «not enough space on your device memory» and «
phone storage is full», even though the sd card I have is with the capacity of 16GB! What should I do to clear phone storage?
The answer. Most likely, the error «not enough memory» arises due to the fact that there isn’t enough internal memory, as this is where the installation files are downloaded from Google Play.
- We advise you to transfer the largest files from Android internal memory to a sd card.
- Uninstall barely used apps via file manager or Files Go.
- Use CCleaner utility to clean your phone memory from unused files.
I was cleaning my phone memory and deleted a lot of files. And now I can’t have a look at the Gallery through Android, as it shows: «Insufficient storage available». How do I fix it?
The answer. Likely, while cleaning you’ve deleted the folder with photos on your memory card (SDCARD/DCIM/CAMERA). You can recover files from there via CardRecovery or PhotoRec.
Android 7. The file system consists of apps and cache. It’s used as a mean to increase the capacity of memory. All downloads are saved in the internal memory. How can I transfer them to a card?
Please, go through the manual on how to save downloaded files on a memory card automatically. In order to do this, you have to change the settings of the browser you’re using and make changes in the Android OS settings.
The phone was lacking internal memory so I transferred half of the photos onto an SD card, then I opened them and saw that they all were blurred.
You might have copied thumbnails instead of the originals onto your memory card. The original photos may still be in the phone internal memory. If they aren’t there, the DiskDigger utility will help you.
There wasn’t enough internal memory on the phone, so I wanted to clear it. I transferred data (photos and music) to a memory card. Now the files are unreadable, although the phone sees the card. How can I return at least the photos?
If there is a lack of space in your device memory, copy the files onto a computer (create a backup copy) and only after this step you can move them to a memory card. We highly recommend reading the manual on how to free internal memory on Android (see the text above).
I have a Sony Xperia, when I open the Play Market willing to download an app, the system notifies that there isn’t enough memory on Android, although a 16 GB flash drive is inserted!
Most likely, the error «not enough memory» on Android is occurring due to the fact that there isn’t enough internal memory and this is the place where installation files from Google Play are downloaded.
- You should transfer the largest files from your Android internal memory to your SD card.
- Or delete unnecessary apps via File Manager or Files Go.
- You can also use the CCleaner utility in order to clear unnecessary files from your phone memory.
When I was cleaning my phone memory, I deleted a lot of folders. And now I can’t open the Gallery via Android as the OS states that «The storage is not available». How can I get the files back?
It’s likely that you have deleted the folder with photos from your memory card (SDCARD/DCIM/CAMERA). You can recover the files from there using the CardRecovery or PhotoRec programs.
I can’t install any app from the Play Market. The Android OS reports that there is no space on the phone, although all the apps have been moved to an SD card and there is definitely enough space on it (9 GB).
You have run out of free space in your phone internal memory. Therefore you have to delete the files stored there. Use any File Manager for this purpose (for example, Total Commander or ES Explorer).
We also recommend deleting unneeded apps from your phone memory or, as another option transfer them to your SD card via the standard Android App Manager.
Источник