zoukankan      html  css  js  c++  java
  • 优化技术之内存消耗测试

    2013-06-29

    内存消耗测试

    当一个Java应用程序运行时,有很多需要消耗内存的因素存在,如对象、加载类、线程等。在这里只考虑程序中的对象所消耗的虚拟机堆空间,这样我们就可以利用Runtime类的freeMemory()和totalMomery()方法。

    public class Handler implements InvocationHandler {

      private Object obj;

      public Handler(Object obj) {

        this.obj = obj;

      }

      public static Object newInstance(Object obj) {

        Object result = Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), new Handler(obj));

        return result;

      }

      public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        Object result;

        try {

          Log.i(“Handler”, "begin method " + method.getName());

          long start = Memory.used();

          result = method.invoke(obj, args);

          long end = Memory.used();

          Log.i(“Handler”, "the method " + method.getName() + “ lasts ” + (end – start) + “ms”);

        } catch (InvocationTargetException e) {

          throw e.getTargetException();

        } catch (Exception e) {

          throw new RuntimeException(“unexpected invocation exception: ” + e.getMessage());

        } finally {

          Log.i(“Handler”, "end method " + method.getName());

        }

      }

    }

    public class Memory {

      public static long used() {

        long total = Runtime.getRuntime().totalMemory();

        long free = Runtime.getRuntime().freeMemory();

        return (total - free);

      }

    }

  • 相关阅读:
    swift init继承问题
    CocoaPods 使用本地代码
    关于Xcode6 Segue 的疑问,没有解决!
    Cocos2d 学习资料推荐
    iOS8中 UILocalNotification 和 UIRemoteNotification 使用注意
    Cocos2d 初学基本知识
    iOS 和 Android 触摸事件传递
    iOS NSOperation的使用
    Android 相机对焦模式
    AES 推荐文章
  • 原文地址:https://www.cnblogs.com/fengzhblog/p/3162370.html
Copyright © 2011-2022 走看看