4

Where do references to Java objects actually exist? (Such as for Garbage collection)

Strong references are not recycled during garbage collection. The hprof file appears to record the connections between objects. Where do they exist in the JVM? I guess there's code to maintain it, too? Does it have anything to do with object headers?

void setView(View view) {
    // This is a “reference” to the view
    this.view = view;
}

Question:

  1. Where do the actual references exist?
  2. If not, where did the hprof file come from?
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
user9214779
  • 309
  • 2
  • 8
  • On the stack as variables, or in the heap inside objects as data members. Both `view` variables in your example are references. – user207421 Aug 29 '19 at 07:54

2 Answers2

4

Where do references to Java objects actually exist?

In memory. Either in the heap, or on a thread stack, or (in some circumstances) in other locations that the JVM knows about. Always in memory.

Strong references are not recycled during garbage collection.

Erm ... not sure what you are really saying here. References are not reclaimed by the GC because they are always held as part of something else.

The hprof file appears to record the connections between objects.

It does.

Where do they exist in the JVM.

The connections are references. If object a has a reference to object b in one of its instance variables, then a will show up as "connected" to b in the hprof file.

But note that the hprof file is a file. Its not memory. It is produced by the JVM examining all of its live memory locations, and producing a snapshot of the objects.

I guess there's code to maintain it, too?

There is code that creates the hprof file. It is part of the JVM.

Does it have anything to do with object headers?

Well sort of. The code that generates the hprof file understands object headers.

Where do the actual references exist?

In memory; see above.

If not, where did the hprof file come from?

The hprof file is written by the JVM or by a tool that talks to the JVM. (I am not 100% sure which, but it doesn't really matter.)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1

Where do references to Java objects actually exist?

Like all other programs, a variable or object resides in memory. A program or function is loaded into the memory before execution along with the function arguments and other variables. Static variables along with function arguments are stored in the stack of the memory layout whereas the dynamic memory allocations are done in the heap segment of the program stack layout. If you are confused about the program stack layout/frame, I would recommend checking this answer here.

The hprof file appears to record the connections between objects. Where do they exist in the JVM?

I am not sure about this one. As far as I know, it creates the dump from the heap when demanded by the program executor. This is just a file which has the heap dump. In the case of Android, this should be created when you run the profiler and add specific instructions to create the heap dump for you. However, I am not certain about this. This is an old video, which might give you an idea on how you can generate a hprof file. The developer's documentation tells you how to create a heap dump as well.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
  • 2
    The "connections" between objects are references. And they exist in memory. – Stephen C Aug 29 '19 at 07:48
  • The heap dump has no relation with the JVM? Pardon? – user207421 Aug 29 '19 at 07:56
  • I understand that came out wrong. IMHO and from my understanding, heap dumps are stored in the file system which does not have a relation to the program memory. Please correct me if I am wrong. – Reaz Murshed Aug 29 '19 at 08:02
  • 1
    @user207421 at least, [they might have far less relationship than most people think…](https://shipilev.net/blog/2014/heapdump-is-a-lie/) – Holger Aug 29 '19 at 11:09
  • This is all irrelevant. Nobody has said anything about instance size here @Holger, and a heap dump is a dump of the heap, which is part of the JVM, and therefore has a considerable relationship with it, and the fact that it is dumped to the file system doesn't change that. – user207421 Aug 29 '19 at 11:27
  • 1
    @user207421 of course, there is a relationship and the answerer already removed that misleading statement. – Holger Aug 29 '19 at 11:45