My cache can reach large proportions. This is a serious problem for me now. Please tell me how to limit the size of the cache. For example, if you use the function getCacheDir(), as it is possible to limit the size of the file
Please tell me how to limit the size of the cache
Use Java file I/O to see how much space you are consuming, then delete some stuff when you exceed some threshold.
if you use the function getCacheDir(), as it is possible to limit the size of the file
You are welcome to measure the size of your file yourself and limit it yourself. There is nothing in Android that will limit the size of your cache.
Related
I am using a DiskLruCache to store around 70k files. These files are really small, half of them are not even 13 bytes each. However each file size is at least 4kb in disk because I assume that's the minimum block size. Therefore when I set a maxSize for the cache of 256MB the cache actually grows above the 450MB in disk.
I modified the code in DiskLruCache so it takes in consideration that files are stored in 4kb blocks.. however, my question is, can I rely that every Android device will always divide its sdcard in 4kb blocks? Could it be that another device has a bigger or smaller block size?
I used StatFs(directory.getAbsolutePath()).getBlockSize()
I'm having a hard time finding an updated answer for this.
So far I've been aiming to land my resources <16mb. But This is getting harder and harder the further along I get, I mean, a 1000x1000 .Png-file uses around 1mb.
Obviously I'm aiming to keep it as small as possible, but at what size should I start worrying? Planning on releasing for android >2.1
The lowest heap space you will ever get is 16 MB, IIRC. It is possible that you may be given upto 64 MB, or even more, but you should always try and operate under 16 MB. Remember that if you can possibly get more heap space, and you do try and use it up you could end up displacing other apps from the memory, and cause some minor or in some cases major problems to users.
It's not that you can't get more space. The fact is that you should make your app as good an Android citizen as possible, and be as friendly to the device as you can.
You can get the maximum healp size with:
Log.d(TAG,"Memory Class: " + ((ActivityManager) getSystemService(ACTIVITY_SERVICE)).getMemoryClass());
Log.d(TAG,"Heap size: " + Runtime.getRuntime().maxMemory());
If you are loading big PNG files, you should look for the best practices on how to handle big images on Android, but have in mind that some of your options for the BitmapFactory have huge impact on how the image is loaded:
-inScaled when set may increase the bitmap memory size by a factor 2 or 3 in new devices with high screen density,
-ARGB type may go from 2 bytes per pixel (RGB_565 and ARGB_4444) to 4 bytes (ARGB_8888), so a factor of 2 again.
good luck.
I want to load a text file in a wordprocessing apps. But I get out of memory error when files are of too big. I finalized that I can load upto 1 MB files. But sometimes I get out of memory even for 1MB files. But I have to say before loading whether I can load files or not.
I tried for solution of finding maximum possible available memory, apart from freeMemory(),
that is freememory + (maxmemory - totalmemory) which will give the total possible available memory for the application. (Say it will be around 18MB to 20 MB). But I get outofMemory error after completely utilizing the heap. say for example(24 MB).
My question is really that (18MB to 20 mb) of "maximum possible available memory" is utilized for allocation when loading 1MB file.
How much of memory should be available to load 1MB file.
How can I roughly compute it.
Is there any way out of PSS, Privatedirty. I couldn't understand much about PSS. But I couldn't get much info regarding of summing up in article "How to discover memory usage of my application in Android"
Thanks
Remember, the way you store the files in variables matters quite a lot. Using char array manually is one of the most memory-efficient ways, but you still need to account for every character taking 16 bits, or 2 bytes. So, if you have text file using some 8-bit encoding and you load it into char array, it takes twice as much space.
i have a weird thing when downloading file from an URL in my program.
I perform check of available updates of files on server by checking same file's size on the device and on the server. When i use getContentLength method of URLConnection class i get one size, then i download and check the downloaded size by bytes which get downloaded and get same size. But when i look at the size of the files on the SDCard they are about 1.4 times bigger. Why does this happen?
So, basically i download one size and get different on SDCard and thats why i can't check if they are updated by asking the server.
URL is of https type, files are .ics
It's possible that it's due to filesystem overhead. I assume you're writing to a FAT32 SD card, in which case there may be significant overhead in some cases.
According to Wikipedia (http://en.wikipedia.org/wiki/File_Allocation_Table):
"Note however, that files are allocated in units of clusters, so if a 1 kB file resides in a 32 kB cluster, 31 kB are wasted."
Consider trying with large files or files of different sizes if possible, and see if the ratio of expected to actual size remains the same.
Solved. The problem was with the encoding. I had to use OutputStreamWriter with secon parameter "UTF8". And that aswell solved the problem with cyrylic letters in the text.
My app needs to save files that will range from about 2-20mb. When I tried to do this I was getting an OutOfMemoryException. I did some reading and it's looking like Android has a file size limit of 1mb. Is this correct? If so, is there a way around this limitation, other than splitting up every file into 1mb chunks?
The main application need to be small, like < 1 MB but you can save as many files as you want and as large as you want as long as you save them on the memory card. The available space that can be used for applications (and other secure data) is limited, usually under 128 MB. So basically you need to keep your application small and put the large part as an add-on or extra files that can be put on the memory card. If you application will use 20MB from the available space it will drastically reduce the number of people that will use it.
OutOfMemoryError means you exceeded the VM's RAM budget, which is 16MB or 24MB depending on what device you're on. It has nothing to do with file sizes.
The 1MB limit you're probably referring to is the maximum size of a compressed asset in an APK file.
Files in your app-private data area or on external storage can be as large as the filesystem will allow them to be. (I've heard the FAT32 implementation Android uses for SD cards has a 2GB limit for individual files, but don't remember the resolution of that thread.) Available disk space will likely be a larger concern.
Going back to your original problem, check the logcat output (via adb logcat or DDMS) to see if there are any messages from the garbage collector right before the OOM fired.