Tell Android not to cache application in memory - android

I'm writing an application using libgdx and am having a weird issue.
When the game loads, it works well.
The moment you exit the game, and load it again - the graphics are all scrambled (ie - the textures its loading are swapped with other textures, so I might have the texture for the ground replaced with the texture for the main character...).
Clearing the game from the history (and I'm assuming from the cache?) means the game loads perfectly on next time.
So is there a way to tell Android (from the configuration I'm assuming) to not cache the application when I 'close' it?

Using these lines in your activity:-
this.finish();
Process.killProcess( Process.myPid() );
will kill your whole application (assuming its running in a single process) it will also free any associated memory.
This may help in game to load perfectly next time at application will be killed completely.

You can use the following code to remove the catch for your application, Call it from onDestroy() of your activity from where you are going to exit from the application
public static void deleteCache(Context context) {
try {
File dir = context.getCacheDir();
if (dir != null && dir.isDirectory()) {
deleteDir(dir);
}
} catch (Exception e) {}
}
public static boolean deleteDir(File dir) {
if (dir != null && dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
return dir.delete();
}

Related

How to clear cache in android version 7.1

Please help with clearing cache in android. I am able to delete cache with below code on android version 6.0. When i am trying it in 7.1 nothing changes. what am i missing?
//method to delete cache- issue reported
public static void deleteCache(Context context) {
try {
File dir = context.getCacheDir();
deleteDir(dir);
Log.d("cache clear", "cache delete "+dir);
} catch (Exception e) { e.printStackTrace();}
}
public static boolean deleteDir(File dir) {
if (dir != null && dir.isDirectory()) {
Log.d("cache clear", "cache delete 1");
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
return dir.delete();
} else if(dir!= null && dir.isFile()) {
return dir.delete();
} else {
return false;
}
}
From Android API 19 you can do that:
((ActivityManager)context.getSystemService(ACTIVITY_SERVICE)).clearApplicationUserData();
Official docs:
clearApplicationUserData
added in API level 19
public boolean clearApplicationUserData ()
Permits an application to erase its own data from disk. This is equivalent to the user choosing to clear the app's data from within the device settings UI. It erases all dynamic data associated with the app -- its private data and data in its private area on external storage -- but does not remove the installed application itself, nor any OBB files. It also revokes all runtime permissions that the app has acquired, clears all notifications and removes all Uri grants related to this application.
Returns
boolean true if the application successfully requested that the application's data be erased; false otherwise.
EDIT
From your Activity don't call
deleteCache(this)
but call
deleteCache(getApplicationContext())

How much memory my app should preserve after closing it

Once I open my application it preserves about 7MB (Via Android Studio Memory Monitor), and during using it (Populating a listView with custom items) it pops to about 9MB : 12MB
After I close the application and use 'Initiate GC' from Android Studio it goes down to about 8.3MB.
So, does this mean / indicates that I have a memory leak?
Shouldn't it goes back to 7MB as it started?
or Shouldn't it be 0MB as my application closed?
If you are looking for delete cache of your own application then simply delete your cache directory and its all done !
public static void deleteCache(Context context) {
try {
File dir = context.getCacheDir();
deleteDir(dir);
} catch (Exception e) {}
}
public static boolean deleteDir(File dir) {
if (dir != null && dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
return dir.delete();
}
else if(dir!= null && dir.isFile())
return dir.delete();
else {
return false;
}
}
And you may require following permission to add in your manifest file in order to delete cache of other application
<uses-permission android:name="android.permission.CLEAR_APP_CACHE"/>
Reference : Clear Cache in Android Application programmatically

Android Picasso clear image from disk and cache

I'm using Picasso for image loading, I use it with NetworkPolicy.OFFLINE,
but at some point I want to update the image from internet and I'm trying with
Picasso.with(context).invalidate(url);
Picasso.with(context).load(url).memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE).networkPolicy(NetworkPolicy.NO_CACHE);
But the image is still taken from disk, only the cache is invalidated.
Use this:
Picasso.with(context).load(url).memoryPolicy(MemoryPolicy.NO_CACHE).into(image);
You can clear cache programmatically using below code. You can use this code before you need to get images from API.
If you want PICASSO to never cache images you can use code shared by waseem. but if you need it to cache but only sometimes you need to get images from API you can clear cache.
public static void trimCache(Context context) {
try {
File dir = context.getCacheDir();
if (dir != null && dir.isDirectory()) {
deleteDir(dir);
}
} catch (Exception e) {
// TODO: handle exception
}
}
public static boolean deleteDir(File dir) {
if (dir != null && dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
return dir.delete();
}
else {
return false;
}
So I was calling this when I did not have any views available yet.
The key is to call .into() method that triggers overwriting the cache.
So I use only this:
Picasso.with(context).load(url).into(new ImageView(context));

Android, memory overuse issue

I am beginner to Android development. There are several screens in my app and I can navigate back and forth between these screen using "backstack" feature. As I traverse through the app in depth, the memory usage of app is increasing. Sometimes it reaches up to 100 MB..!! It causes app to restart.
As I understand, below may be, possible reasons behind memory overuse:
1) App makes heavy use of images.
2) Backstacking is implemented. (There is only once activity used and other are fragments)
3) There is one background service running continuously for that app.
I am not sure whether these are the actual reasons. Please help me to find out the reasons. Any tips regarding memory overuse problem? One more question, What should be the ideal memory usage of Android app?
call these two method in your app it will delete all cache of images from android data and ur app will not crashed from memory issue
//this is for clear cache folder in android -> data folder which generate by gallry lazy loading
public static void trimCache(Context context) {
try {
// File dir = context.getCacheDir();
File dir = context.getExternalCacheDir();
if (dir != null && dir.isDirectory()) {
deleteDir(dir);
System.out.println("delete cache folderrrrrrrrrr");
}
} catch (Exception e) {
// TODO: handle exception
}
}
public static void trimCacheinternal(Context context) {
try {
File dir = context.getCacheDir();
// File dir = context.getExternalCacheDir();
if (dir != null && dir.isDirectory()) {
deleteDir(dir);
System.out.println("delete cache folderrrrrrrrrr");
}
} catch (Exception e) {
// TODO: handle exception
}
}

Delete cached files - WebView Android 4.4+

I managed to delete cached files created by WebView using:
Clearing android cache,
Clear Application cache on exit in android
However for Android 4.4 that solution doesn't work properly, since the files are cached in:
/data/data/com.app.package/app_webview/
instead of:
/data/data/com.app.package/cache/
The above path can be obtained by the official command getCacheDir().
An approach could be hard-coding the path obtained through Get Application Directory
However, is there any [official]/proper solution to address this?
you can use this code
private static boolean deleteDir(File dir) {
if (dir != null && dir.isDirectory()) {
String[] children = dir.list();
for (String aChildren : children) {
boolean success = deleteDir(new File(dir, aChildren));
if (!success) {
return false;
}
}
}
// The directory is now empty so delete it
return dir != null && dir.delete();
}
void trimCache() {
try {
String pathadmob = this.getFilesDir().getParent() + "/app_webview";
File dir = new File(pathadmob);
if (dir.isDirectory()) {
deleteDir(dir);
}
} catch (Exception e) {
e.printStackTrace();
}
}
Also, here was generated all admob cache 4.4+, you can use a code to verify how many times the user use the app, and delete the admob cache when the user reached the limit.
Normally, to clear WebView cache, use this WebView API: WebView.clearCache(true);

Categories

Resources