如何计算android.graphics.Path的内存使用率

如何计算android.graphics.Path的内存使用率,android,memory-management,Android,Memory Management,我正在使用LRUCache缓存android.graphic.path对象。为此,我需要知道路径用于覆盖LRUCache的sizeOf()方法的内存量 所以我的问题是:如何计算路径的内存使用率 我已经面临的困难是: android.graphic.path不可序列化 路径可能存储为位图,我已经读了什么 正如我在下面的代码中所做的那样,对内存使用情况进行初步测量,结果令人困惑。例如: 具有10000行的路径使用312字节 包含10行的10个路径,每个路径使用992字节 有什么提示吗 Ar

我正在使用LRUCache缓存android.graphic.path对象。为此,我需要知道路径用于覆盖LRUCache的sizeOf()方法的内存量

所以我的问题是:如何计算路径的内存使用率

我已经面临的困难是:

  • android.graphic.path不可序列化
  • 路径可能存储为位图,我已经读了什么
正如我在下面的代码中所做的那样,对内存使用情况进行初步测量,结果令人困惑。例如:

  • 具有10000行的路径使用312字节
  • 包含10行的10个路径,每个路径使用992字节
有什么提示吗


ArrayList alPathes=new ArrayList();
long iAnzahlBigPathes=1;
long iAnzahlOperation4BigPath=20000;
gc();gc();gc();gc();gc();gc();gc();gc();
long lused1=getUsedMemory();
Log.v(“Memory4operation”,“ltotal1”+lused1);//getMemoryStatus

对于(int o=0;oPath,如果可用,通常会使用硬件渲染。因此,在内部,它不是存储为位图,而是一组“绘图说明”。 您必须查看Path的底层本机实现(SkPath.cpp),以了解路径在内部的存储方式

似乎没有API可用于获取路径的大小。 剩下的是:

  • 查看实现并自己估计。请记住,如果将Path对象保存到缓存,则缓存的是指令,而不是呈现的结果
  • 在将路径放入LRUCache之前,将其渲染为位图。这将节省渲染时间。尤其是在绘制路径成本较高的情况下。无论如何,这很可能是您想要的
  • ArrayList<Path> alPathes=new ArrayList<Path>();
    long iAnzahlBigPathes=1;
    long iAnzahlOperation4BigPath=20000;
    
    System.gc();System.gc();System.gc();System.gc();System.gc();System.gc();System.gc();System.gc();
    long lused1=getUsedMemory();
    Log.v("Memory4operation","ltotal1 " + lused1);// getMemoryStatus
    
    for(int o=0;o<iAnzahlBigPathes;o++){
        Path path=getBigPath(iAnzahlOperation4BigPath);
    
    
        int iSize=path.toString().length();
    
        alPathes.add(path);
    }
    System.gc();System.gc();System.gc();System.gc();System.gc();System.gc();System.gc();System.gc();
    long lused2=getUsedMemory();
    Log.v("Memory4operation","ltotal2 " + lused2);// getMemoryStatus
    
    long iDifferenz=lused2-lused1;
    Log.v("Memory4operation", "Memory used for path " + iDifferenz + " in bytes");
    
    //float fMemoryforoneline=(float)iDifferenz/(float)iAnzahlSquares;
    float fMemoryforoneoperation=(float)iDifferenz/(float)iAnzahlOperation4BigPath/(float)iAnzahlBigPathes;
    Log.v("Memory4operation", "" + fMemoryforoneoperation);
    
    
    public long getUsedMemory(){
        final int ltotal = (int) (Runtime.getRuntime().totalMemory());
        final int lfree = (int) (Runtime.getRuntime().freeMemory());
        return (ltotal - lfree);
    }
    
    public Path getBigPath(long iAnzahlOperation4BigPath){
    
        int imin=10;
        int imax=1000000;
        Path path=new Path();
        path.moveTo(randInt(imin,imax),  randInt(imin,imax));
        for(int i=0;i<iAnzahlOperation4BigPath;i++){
            path.lineTo(randInt(imin,imax), randInt(imin,imax));
        }
        path.close();
        return path;
    }