javauis/javalegacyutils/javasrc/com/nokia/mj/impl/rt/legacy/MemoryUtil.java
changeset 76 4ad59aaee882
parent 69 773449708c84
child 79 2f468c1958d0
equal deleted inserted replaced
69:773449708c84 76:4ad59aaee882
     1 /*
       
     2 * Copyright (c) 2001-2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 package com.nokia.mj.impl.rt.legacy;
       
    20 
       
    21 import com.nokia.mj.impl.rt.support.JvmInternal;
       
    22 import com.nokia.mj.impl.rt.support.Jvm;
       
    23 
       
    24 public class MemoryUtil
       
    25 {
       
    26 
       
    27     static
       
    28     {
       
    29         Jvm.loadSystemLibrary("javalegacyutils");
       
    30     }
       
    31 
       
    32 
       
    33     // Two independent mechanisms are available for managing native
       
    34     // heap consumption.
       
    35 
       
    36     // The first mechanism checks available system heap, and if
       
    37     // beneath a configurable limit forces a garbage collection
       
    38     // followed by synchronous execution of all pending
       
    39     // finalizers. This mechanism is not used by Symbian JSRs but
       
    40     // is preferred by some licensees.
       
    41 
       
    42     private static final int FREE_KB_RAM_LIMIT_FOR_FINALIZATION = 4096; // S60 change
       
    43 
       
    44     public static void ensureMinFreeRAM()
       
    45     {
       
    46         final long freeRAM = _getFreeRAMInKB();
       
    47         if (freeRAM < FREE_KB_RAM_LIMIT_FOR_FINALIZATION)
       
    48         {
       
    49             // Attempt GC also if returned an error code (!= KErrNone)
       
    50             freeNativeMemory();
       
    51         }
       
    52     }
       
    53 
       
    54     // The second mechanism allows native allocation/deallocation
       
    55     // to be tracked and triggers regular garbage collections
       
    56     // after a configurable delta. This is the mechanism used by
       
    57     // Symbian JSRs since it is proactive in keeping native heap
       
    58     // allocation under control rather than allowing the
       
    59     // consumption of all system memory before taking action.
       
    60 
       
    61     private static final int NATIVE_ALLOCATION_GC_THRESHOLD = 2048 * 1024; // S60 change
       
    62 
       
    63     static final Object sNativeAllocationLock = new Object();
       
    64     static long sNativeAllocationCounter = 0;
       
    65 
       
    66 
       
    67     /**
       
    68      * Native heap management memory allocated notification.
       
    69      *
       
    70      * Called after a native memory allocation to allow
       
    71      * heap manager to track native heap usage.
       
    72      */
       
    73     public static void nativeMemoryAllocated(int aNativeBytes)
       
    74     {
       
    75         /*
       
    76          * Native heap allocation algorith: track known native allocations and
       
    77          * deletions. Trigger GC when total allocation reaches a threshold.
       
    78          * We zero the allocation count when this happens to avoid pathological
       
    79          * cases for applications that have a stable native heap use that just
       
    80          * exceeds the threshold.
       
    81          */
       
    82         boolean gc = false;
       
    83         synchronized (sNativeAllocationLock)
       
    84         {
       
    85             sNativeAllocationCounter += aNativeBytes; // S60 change
       
    86             if (sNativeAllocationCounter > NATIVE_ALLOCATION_GC_THRESHOLD)   // S60 change
       
    87             {
       
    88                 sNativeAllocationCounter = 0;
       
    89                 gc = true;
       
    90             }
       
    91         }
       
    92         if (gc)
       
    93         {
       
    94             freeNativeMemory(); // S60 change
       
    95         }
       
    96     }
       
    97 
       
    98     /**
       
    99      * Native heap management memory released notification.
       
   100      *
       
   101      * Called after native memory freed to allow heap manager
       
   102      * to track native heap usage.
       
   103      */
       
   104     public static void nativeMemoryFreed(int aNativeBytes)
       
   105     {
       
   106         /*
       
   107          * Decrement native heap counter and zero clamp.
       
   108          */
       
   109         synchronized (sNativeAllocationLock)
       
   110         {
       
   111             sNativeAllocationCounter -= aNativeBytes; // S60 change
       
   112             if (sNativeAllocationCounter < 0)
       
   113             {
       
   114                 sNativeAllocationCounter = 0;
       
   115             }
       
   116         }
       
   117     }
       
   118 
       
   119     /*
       
   120      * Attempt to free native peers by forcing GC and finalization.
       
   121      */
       
   122     public static void freeNativeMemory()
       
   123     {
       
   124         //attempt GC also if returned an error code (!= KErrNone)
       
   125         if (JvmInternal.runYoungGenerationGc())
       
   126         {
       
   127             JvmInternal.runFinalization();
       
   128 
       
   129             if (_getFreeRAMInKB() >= FREE_KB_RAM_LIMIT_FOR_FINALIZATION)
       
   130             {
       
   131                 return;
       
   132             }
       
   133         }
       
   134         System.gc();
       
   135         JvmInternal.runFinalization();
       
   136     }
       
   137 
       
   138     private static native long _getFreeRAMInKB();
       
   139 }
       
   140 // eof
       
   141