How do I know the updated available heap size?

I am having difficulty getting the updated available application heap size after deleting some of the large objects.

My requirement is to free memory as soon as the user reaches a certain heap level. for example, I am using a Samsung Tab3 with a 64MB heap size for an application.

The app shouldn't go out of memory while viewing images, so I capped 55MB as the maximum limit for heap growth. I am looking at the available heap size before rendering the image. If the heap is greater than 55MB, I delete some of the recently viewed images, so I can get enough memory to load the image.

But the problem is that after deleting the image objects, I got the last increased heap size, which is always over 55MB. I also called gc after deleting each image, but has no effect.

I want to reduce the size of the heap after deleting the image object.

if the heap has reached 55MB, then for each heap the delete should decrease, how to get the reduced heap size?

I am using the following codes to get the heap size.

/**
     * This method is used to get currently allocated heap size to application.
     */
    public static int getAllocatedHeapSize()
    {
        DecimalFormat df = new DecimalFormat();
        int size = new Double(Runtime.getRuntime().totalMemory()/1048576).intValue();
        Log.d("heap", "debug.memory: allocated: " + size + "MB of " + df.format(new Double(Runtime.getRuntime().maxMemory()/1048576))+ "MB (" + df.format(new Double(Runtime.getRuntime().freeMemory()/1048576)) +"MB free)");
        return size;
    }

/**
     * Check whether free memory is available to store new attachment page
     * @return true if available else false
     */
    public static boolean isFreeMemoryAvailable()
    {
        int allocatedHeapSize = getAllocatedHeapSize();
        if (allocatedHeapSize > ) {
            return false;
        }

        return true;
    }

      

The isFreeMemoryAvailable () method goes on endlessly because it does not receive the updated heap size.

Give me a solution as soon as possible.

+3


source to share


2 answers


Runtime.totalMemory()

does not depend on how much of the heap is actually used in the call. It returns the actual size of the heap. From the doc:

Returns the number of bytes made by the heap at the current size.

Deleting objects does not necessarily change the size of the heap.



You can get the actual heap usage by running:

Runtime.totalMemory() - Runtime.freeMemory();

      

However, you might want to look at saving images to android.util.LruCache

or something instead of managing this yourself.

+2


source


There are several aspects to this question:

1) You can get more reliable and detailed statistics by using java management beans like http://docs.oracle.com/javase/7/docs/api/java/lang/management/MemoryMXBean.html among others (MemoryMXBean, MemoryPoolMXBean , GarbageCollectorMXBean)



2) You cannot reduce the heap size from a java application, you can only reduce the heap usage.

3) You might consider using Soft References http://docs.oracle.com/javase/7/docs/api/java/lang/ref/SoftReference.html . This class allows references to be retained until space is otherwise required, and collected only when needed. This way you can limit the size of the heap and use soft links to store the data as long as possible, but not prevent the GC from collecting it if needed.

+1


source







All Articles