zoukankan      html  css  js  c++  java
  • java中打印变量地址

    在java中打印变量的地址

    这个代码是在startoverflow上看到的,跟大家分享一下。

    import sun.misc.Unsafe;
    
    import java.lang.reflect.Field;
    import java.util.Arrays;
    import java.util.Collections;
    public class OrderOfObjectsAfterGCMain {
        static final Unsafe unsafe = getUnsafe();
        static final boolean is64bit = true; // auto detect if possible.
    
        public static void main(String... args) {
            Double[] ascending = new Double[16];
            for(int i=0;i<ascending.length;i++)
                ascending[i] = (double) i;
    
            Double[] descending = new Double[16];
            for(int i=descending.length-1; i>=0; i--)
                descending[i] = (double) i;
    
            Double[] shuffled=new Double[16];
            for(int i=0;i<shuffled.length;i++)
                shuffled[i] = (double) i;
            Collections.shuffle(Arrays.asList(shuffled));
    
            System.out.println("Before GC");
            printAddresses("ascending", ascending);
            printAddresses("descending", descending);
            printAddresses("shuffled", shuffled);
    
            System.gc();
            System.out.println("
    After GC");
            printAddresses("ascending", ascending);
            printAddresses("descending", descending);
            printAddresses("shuffled", shuffled);
    
            System.gc();
            System.out.println("
    After GC 2");
            printAddresses("ascending", ascending);
            printAddresses("descending", descending);
            printAddresses("shuffled", shuffled);
        }
    
        public static void printAddresses(String label, Object... objects) {
            System.out.print(label + ": 0x");
            long last = 0;
            int offset = unsafe.arrayBaseOffset(objects.getClass());
            int scale = unsafe.arrayIndexScale(objects.getClass());
            switch (scale) {
            case 4:
                long factor = is64bit ? 8 : 1;
                final long i1 = (unsafe.getInt(objects, offset) & 0xFFFFFFFFL) * factor;
                System.out.print(Long.toHexString(i1));
                last = i1;
                for (int i = 1; i < objects.length; i++) {
                    final long i2 = (unsafe.getInt(objects, offset + i * 4) & 0xFFFFFFFFL) * factor;
                    if (i2 > last)
                        System.out.print(", +" + Long.toHexString(i2 - last));
                    else
                        System.out.print(", -" + Long.toHexString( last - i2));
                    last = i2;
                }
                break;
            case 8:
                throw new AssertionError("Not supported");
            }
            System.out.println();
        }
    
        private static Unsafe getUnsafe() {
            try {
                Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
                theUnsafe.setAccessible(true);
                return (Unsafe) theUnsafe.get(null);
            } catch (Exception e) {
                throw new AssertionError(e);
            }
        }
    }

    设置因为导入import sun.misc.Unsafe;引起的错误

  • 相关阅读:
    CentOS7中使用yum安装Nginx的方法
    Flask&&人工智能AI --4
    Flask&&人工智能AI --3
    Flask&&人工智能AI --2
    Flask&&人工智能AI --1
    Linux--8
    Linux--7
    django 请求生命周期
    Linux--6 redis订阅发布、持久化、集群cluster、nginx入门
    Node.js Addons翻译(C/C++扩展)
  • 原文地址:https://www.cnblogs.com/CentForever/p/4942616.html
Copyright © 2011-2022 走看看