Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding all references to a specific instance of an object in Android Studio

Is there a way to find all references to a specific instance of an object? from what I understand, while debugging, you get the address of every variable (for example, I have this={ListView@<Number>}. I assume the number is an address)

I've tried using Find all references to object, but it doesn't work (at the bottom of the IDE - not in the log - I've noticed something that says the object doesn't support that operation, and not surprisingly - it says "Collecting data" but I get nothing).

Is there any other way/trick? I can set a breakpoint to find the object, but when I go back in the stacktrace, it becomes harder to find the variable, since it's hidden inside other objects which are inside other objects etc.

Also, following the lines the stacktrace points to is a problem, since it doesn't actually point to the right lines...

like image 682
user1999728 Avatar asked Sep 23 '15 13:09

user1999728


2 Answers

This might be a bit late, but for anyone else looking for an answer - while debugging in Android Studio, right click on the object's instance and select Show Referring Objects.... This will print a list of all objects that hold a reference to your object.

like image 80
Łukasz Paczos Avatar answered Nov 11 '22 20:11

Łukasz Paczos


If you can insert a breakpoint at a place where the object is in scope and not nested too deeply within other objects, then Łukasz Paczos's answer is the way to go.

An alternative approach is to use Android Studio's Memory Profiler, as follows:

  1. Connect a device which allows USB debugging.
  2. Run your app on the device.
  3. Open the "Profiler" window in Android Studio.
  4. Start a profiling session for the app that's running.
  5. Tap into the "Memory" profiler.
  6. Select the "Capture heap dump" radio button.
  7. Tap "Record".
  8. In the heap dump's list of classes, find and tap on the class that you want to inspect.
  9. In the "Instance List" pane, tap on one of the instances.
  10. In the "Instance Details" pane, tap the "References" tab.

Steps 3-5 are demonstrated in the following screenshot:

Screenshot demonstrating steps 3 to 5

Steps 6-7 are demonstrated in the following screenshot:

Screenshot demonstrating steps 6 and 7

Steps 8-10 are demonstrated in the following screenshot:

Screenshot demonstrating steps 8 to 10

like image 35
Adil Hussain Avatar answered Nov 11 '22 22:11

Adil Hussain