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()
Related
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.
Describing background, as I may just have a terrible approach to the problem - self learning.
I'm writing an app for android, and testing it on default AVD, which is set to WVGA800 with 512 'device ram size' and 240 'Abstracted LCD density'.
I have some images, and I put them into drawable-hdpi.
There are 458 KB (not MB) worth of images in that folder.
All images are in PNG format.
The issue is that when I try to load my biggest image (used for background), it throws: java.lang.OutOfMemoryError
This is the call to load the image:
BitmapFactory.decodeResource(status.getResources(), R.drawable.background);
This is identical to how I load the rest of images (33 in total).
It makes sense to me, that it would run out of memory on biggest image, but my total size of folder is 458 KB, so I wouldn't expect to run out of 512 MB Ram set on device.
I never unload any images, I keep them loaded, and use as needed.
I wrote a different app before, where my total size of images was 563 KB with 82 images total, and I didn't have this issue (using the same AVD). In fact the prior app used to make a couple of copies of each image by flipping it, and still didn't run out of space. Current app is failing on initial load - before much happened.
Could someone point me at what the issue could possibly be? And how I can solve it, or maybe mention if my approach is wrong (self-teaching myself from examples)
I'll give you some hints on how i manage to lessen that problem
If you plan to support all devices, put all your resource into the xhdpi folder. especially the background
File size != memory size
Keep in mind a few things:
Your application has a memory limit (this depends on the android version). You don't get all the device memory. I think that first android version have a memory limit of 16mb.
The size of the file doesn't represent the size of the bitmap in memory. For example a 32bit ARGB bitmap will take 32*width*height bits
If you are dealing with big images then scale them first. Calculate the size you need (this will probably be the size of your ImageView) and load a resized copy of the bitmap. You can do this using BitmapFactory.Options
Yea, this is a pretty common problem. So in older versions of android OS, the bitmap was loaded into native memory and not the JVM. The garbage collection process would really have 2 cycles. One to clear out the memory in the JVM and the other to clear out the memory in the native memory (for bitmaps). If you want to work on older devices, you will need to handle this situation by either recycling your bitmaps Bitmap.recycle() or calling System.gc()
There are two problems that you might be hitting:
1. You have other bitmaps that are un recycled.
2. You really are running out of memory because that single image is too big. (Make sure the other images are correctly recycled or gc'd so that it doesn't add to the memory footprint). In this case, no much you can do.
ALso, as mehmet suggested, you can read this
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.