Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 35664fa3 authored by Steven Terrell's avatar Steven Terrell Committed by Android (Google) Code Review
Browse files

Merge "Add Count Class Instance API" into main

parents 12e325e6 1ebd7315
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -33913,6 +33913,10 @@ package android.os {
    method @Deprecated public static int getGlobalFreedCount();
    method @Deprecated public static int getGlobalFreedSize();
    method @Deprecated public static int getGlobalGcInvocationCount();
    method @FlaggedApi("android.os.count_class_instances_api") public static long getInstanceCount(@NonNull Class);
    method @FlaggedApi("android.os.count_class_instances_api") public static long getInstanceCount(@NonNull Class, boolean);
    method @FlaggedApi("android.os.count_class_instances_api") @NonNull public static long[] getInstanceCounts(@NonNull java.util.List<java.lang.Class>);
    method @FlaggedApi("android.os.count_class_instances_api") @NonNull public static long[] getInstanceCounts(@NonNull java.util.List<java.lang.Class>, boolean);
    method public static int getLoadedClassCount();
    method public static void getMemoryInfo(android.os.Debug.MemoryInfo);
    method public static long getNativeHeapAllocatedSize();
+114 −5
Original line number Diff line number Diff line
@@ -50,6 +50,7 @@ import java.lang.annotation.Target;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@@ -118,6 +119,11 @@ public final class Debug
        "app_info",
    };

    /*
     * Default return value for getInstanceCounts methods.
     */
    private static final long[] EMPTY_COUNT_CLASS_INSTANCES = new long[0];

    /**
     * This class is used to retrieved various statistics about the memory mappings for this
     * process. The returned info is broken down by dalvik, native, and other. All results are in kB.
@@ -2209,13 +2215,116 @@ public final class Debug
    public static native void dumpNativeMallocInfo(FileDescriptor fd);

    /**
      * Returns a count of the extant instances of a class.
     * Counts the number of instances of the specified class.
     *
     * <p><b>Warning:</b> This operation can be expensive, as it may involve traversing portions
     * of the Java heap. It is primarily intended for debugging purposes and should not be used in
     * performance-critical code.
     *
     * <p>It's intended to help developers identify object leaks or to validate
     * the number of instances of a specific class during development.
     *
     * <p>It is the caller's responsibility to do GC if they don't want unreachable
     * objects to get counted.
     *
     * @param cls {@link Class} the class to count instances of.
     *
     * @return the number of matching instances.
     *
     * @hide
     */
    @UnsupportedAppUsage
    public static long countInstancesOfClass(Class cls) {
        return VMDebug.countInstancesOfClass(cls, true);
    @FlaggedApi(Flags.FLAG_COUNT_CLASS_INSTANCES_API)
    public static long getInstanceCount(@NonNull Class cls) {
        return getInstanceCount(cls, true);
    }
    /**
     * Counts the number of instances of the specified classes.
     *
     * <p><b>Warning:</b> This operation can be expensive, as it may involve traversing portions
     * of the Java heap. It is primarily intended for debugging purposes and should not be used in
     * performance-critical code.
     *
     * <p>It's intended to help developers identify object leaks or to validate
     * the number of instances of a specific class during development.
     *
     * <p>It is the caller's responsibility to do GC if they don't want unreachable
     * objects to get counted.
     *
     * @param classes An array of {@link Class} objects representing the classes to count instances
     *                of. Must not be null.
     *
     * @return An array of {@code long} values, where each element corresponds to the number of
     *         instances of the class at the same index in the {@code classes} list.
     *         The returned array will have the same length as the input {@code classes} array.
     *         If the system is under memory pressure and unable to allocate the return array a
     *         empty array will be returned.
     */
    @FlaggedApi(Flags.FLAG_COUNT_CLASS_INSTANCES_API)
    public static @NonNull long[] getInstanceCounts(@NonNull List<Class> classes) {
        return getInstanceCounts(classes, true);
    }

    /**
     * Counts the number of instances of the specified class.
     *
     * <p><b>Warning:</b> This operation can be expensive, as it may involve traversing portions
     * of the Java heap. It is primarily intended for debugging purposes and should not be used in
     * performance-critical code.
     *
     * <p>It's intended to help developers identify object leaks or to validate
     * the number of instances of a specific class during development.
     *
     * <p>It is the caller's responsibility to do GC if they don't want unreachable
     * objects to get counted.
     *
     * @param cls {@link Class} the class to count instances of.
     * @param includeAssignable if true, any instance whose class is assignable to
     *                   {@code cls}, as defined by {@link Class#isAssignableFrom},
     *                   is counted. If false, only instances whose class is
     *                   equal to {@code cls} are counted.
     *
     * @return the number of matching instances.
     *
     */
    @FlaggedApi(Flags.FLAG_COUNT_CLASS_INSTANCES_API)
    public static long getInstanceCount(@NonNull Class cls, boolean includeAssignable) {
        return VMDebug.countInstancesOfClass(cls, includeAssignable);
    }

    /**
     * Counts the number of instances of the specified classes.
     *
     * <p><b>Warning:</b> This operation can be expensive, as it may involve traversing portions
     * of the Java heap. It is primarily intended for debugging purposes and should not be used in
     * performance-critical code.
     *
     * <p>It's intended to help developers identify object leaks or to validate
     * the number of instances of a specific class during development.
     *
     * <p>It is the caller's responsibility to do GC if they don't want unreachable
     * objects to get counted.
     *
     * @param classes An array of {@link Class} objects representing the classes to count instances
     *                of. Must not be null.
     * @param includeAssignable if true, any instance whose class is assignable to
     *                   {@code cls}, as defined by {@link Class#isAssignableFrom},
     *                   is counted. If false, only instances whose class is
     *                   equal to {@code cls} are counted.
     *
     * @return An array of {@code long} values, where each element corresponds to the number of
     *         instances of the class at the same index in the {@code classes} list.
     *         The returned array will have the same length as the input {@code classes} array.
     *         If the system is under memory pressure and unable to allocate the return array a
     *         empty array will be returned.
     */
    @FlaggedApi(Flags.FLAG_COUNT_CLASS_INSTANCES_API)
    public static @NonNull long[] getInstanceCounts(@NonNull List<Class> classes,
            boolean includeAssignable) {
        long[] instanceCount =
                VMDebug.countInstancesOfClasses(classes.toArray(new Class[0]), includeAssignable);
        if (instanceCount != null) {
            return instanceCount;
        }
        return EMPTY_COUNT_CLASS_INSTANCES;
    }

    /**
+2 −2
Original line number Diff line number Diff line
@@ -477,7 +477,7 @@ public class ViewDebug {
     */
    @UnsupportedAppUsage
    public static long getViewInstanceCount() {
        return Debug.countInstancesOfClass(View.class);
        return Debug.getInstanceCount(View.class);
    }

    /**
@@ -489,7 +489,7 @@ public class ViewDebug {
     */
    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
    public static long getViewRootImplCount() {
        return Debug.countInstancesOfClass(ViewRootImpl.class);
        return Debug.getInstanceCount(ViewRootImpl.class);
    }

    /**