java中的对象图是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2046761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:58:15  来源:igfitidea点击:

What is object graph in java?

java

提问by giri

Whenever I study Garbage Collector I hear the term object Graph. What does it mean exactly?

每当我学习垃圾收集器时,我都会听到术语对象图。它到底是什么意思?

采纳答案by Chandra Patni

Objects have references to other objects which may in turn have references to more objects including the starting object. This creates a graph of objects, useful in reachability analysis. For instance, if the starting object is reachable (say it's in a thread's local stack) then all objects in the graph are reachable and an exact garbage collector cannot harvest any of these objects. Similarly, starting with a set of live objects (roots) if we create a list of all reachable objects, all other objects are garbage - fair game for collection.

对象引用了其他对象,而其他对象又可能引用了更多对象,包括起始对象。这将创建一个对象图,在可达性分析中很有用。例如,如果起始对象是可达的(比如它在线程的本地堆栈中),那么图中的所有对象都是可达的,并且精确的垃圾收集器无法收集这些对象中的任何一个。类似地,从一组活动对象(根)开始,如果我们创建一个所有可访问对象的列表,所有其他对象都是垃圾——公平的收集游戏。

回答by Suraj Chandran

Object graph is basically a dependency graph between objects

对象图基本上是对象之间的依赖图

It is used to determine which objects are reachable and which not, so that all unreachable objects could be made eligible for garbage collection.

它用于确定哪些对象可访问,哪些不可访问,以便所有不可访问的对象都可以进行垃圾回收。

回答by Tom Neyland

An 'Object Graph' is the conceptualization of all instances of the objects from your object model (the classes in your program) and their interconnections.

“对象图”是对象模型(程序中的类)中对象的所有实例及其互连的概念化。

Take for example:

举个例子:

You have two classes

你有两个班级

Class Foo
{
    String aString = "foo";
    Bar aBar;
}

Class Bar
{
    String aString = "boo";
}

If you were to create an instance, Foo myFooand then create an instance of Bar myBar, and connect them, myFoo.aBar = myBar;, your object graph would consist of a single instance of Foowith a reference to a single instance of Bar.

如果您要创建一个实例,Foo myFoo然后创建一个 的实例Bar myBar,并将它们连接起来myFoo.aBar = myBar;,那么您的对象图将包含一个 的单个实例,Foo并引用一个 的单个实例Bar

The garbage collector essentially uses the object graph to determine which instances in memory are still linked to something and possibly needed by the program, and which instances are no longer accessible and therefore can be deleted.

垃圾收集器本质上使用对象图来确定内存中的哪些实例仍然链接到程序可能需要的内容,以及哪些实例不再可访问并因此可以删除。



Someone on wikipediaputs it more eloquently than me:

维基百科上有人比我说得更有说服力:

Object-oriented applications contain complex webs of interrelated objects. Objects are linked to each other by one object either owning or containing another object or holding a reference to another object. This web of objects is called an object graph and it is the more abstract structure that can be used in discussing an application's state.

面向对象的应用程序包含相互关联的对象的复杂网络。对象通过一个拥有或包含另一个对象或持有对另一个对象的引用的对象相互链接。这个对象网络称为对象图,它是更抽象的结构,可用于讨论应用程序的状态。

回答by Stephen C

What we are talking about is the mathematical notion of a directed graph that consists of nodes and edges that connect the nodes. An object graph is some graph whose nodes are objects, and whose edges are relationship of interest between the objects.

我们正在谈论的是由节点和连接节点的边组成的有向图的数学概念。对象图是一些图,其节点是对象,边是对象之间感兴趣的关系。

In the case of the Java garbage collector, the object graph of concern is the graph of reachable objects. In this graph, the nodes are Java objects, and the edges are the explicit or implied references that allow a running program to "reach" other objects from a given one. (For example of an implied reference, there is an implied reference from an object to it's Class object, and hence to the heap objects holding the classes statics and its code ... but I digress.)

在 Java 垃圾收集器的情况下,关注的对象图是可达对象的图。在该图中,节点是 Java 对象,边是显式或隐式引用,允许运行程序从给定对象“到达”其他对象。(例如,一个隐含的引用,从一个对象到它的 Class 对象有一个隐含的引用,因此也有一个包含类静态及其代码的堆对象......但我离题了。)

As @Chandra Patni explained, garbage collection works by traversing the reachability graph consisting of all objects that can be reached from one of a set of starting points; the "root set" in GC terminology. Any object that is not found in this graph traversal can no longer influence the computation, and is therefore eligible to be garbage collected.

正如@Chandra Patni 解释的那样,垃圾收集的工作原理是遍历由一组起点中的一个可以到达的所有对象组成的可达性图;GC 术语中的“根集”。在这个图遍历中没有找到的任何对象都不能再影响计算,因此有资格被垃圾收集。

回答by Ashraf.Shk786

As we know Objects are instance of a Class. An Object may have reference to the other object (use of Pointers for addressing). Also these objects may have reference to another object and so on leading into a Hierarchy of Objects references to each other.

众所周知,对象是类的实例。一个对象可能引用另一个对象(使用指针进行寻址)。此外,这些对象可能具有对另一个对象的引用等,从而导致对象层次结构相互引用。

This is an Object Graph.

这是一个对象图。

回答by Dari

Object graph is a network of instances of classes of our application/software that currently exist in memory.

对象图是当前存在于内存中的应用程序/软件类的实例网络。

Click to see image: http://blog.ploeh.dk/content/binary/Windows-Live-Writer/Compose-object-graphs-with-confidence_921A/Tree_1.png

点击查看图片:http: //blog.ploeh.dk/content/binary/Windows-Live-Writer/Compose-object-graphs-with-confidence_921A/Tree_1.png

It can be a long chain of objects to a short one object graph also. E.g.: lets say we have classes like PetDog, Owner, PetKennel in a application. Now, PetDog has owner, Owner have one or many PetDog, PetDog is trained from a PetKennel and a PetKennel trains many PedDog. Now on implementation of those relationships in Object Oriented Approach we, a Owner (lets say you: a instance/object of Owner class) might reference (link to) many PetDog instances (if you have many dogs else you reference to only one PetDog), again a PetDog references to its particular owner instance/object (that is you in your's dogs case, Mr. John will be referenced by (linked to) his dog), you might have bought pet dog from different kennel club (where dogs are trained and sold also) then each of PetDog instances/objects references/linked to their particular Kennel clubs. This creates a complex network of objects related to each other.

它也可以是一长串对象到一个短的单对象图。例如:假设我们在应用程序中有像 PetDog、Owner、PetKennel 这样的类。现在,宠物狗有主人,主人有一只或多只宠物狗,宠物狗是从宠物犬舍训练出来的,而宠物犬舍会训练许多宠物犬。现在,在面向对象方法中实现这些关系时,我们,所有者(假设您:所有者类的实例/对象)可能会引用(链接到)许多 PetDog 实例(如果您有很多狗,那么您只引用一个 PetDog) ,再次 PetDog 引用其特定所有者实例/对象(即您在您的狗的情况下,约翰先生将被(链接到)他的狗引用),您可能已经从不同的狗舍俱乐部(狗所在的地方)购买了宠物狗训练和出售)然后每个 PetDog 实例/对象引用/链接到他们特定的犬舍俱乐部。

If you happen to represent each instances/objects (each objects of PetDog, Owner, PetKennel) as circle/square (or any shape) in your note/sketch book and draw arrow or lines to represent who object is linked (referencing) with which object then you create a object graph.

如果您碰巧在笔记/草图簿中将每个实例/对象(PetDog、Owner、PetKennel 的每个对象)表示为圆形/正方形(或任何形状),并绘制箭头或线条来表示对象与哪个对象相关联(引用) object 然后你创建一个对象图。

Sometime it happens that when you delete or change links between those instances of any class then some instances might not be referenced (linked) to any other instances which will be removed by garbage collector.

有时,当您删除或更改任何类的那些实例之间的链接时,某些实例可能不会被引用(链接)到将被垃圾收集器删除的任何其他实例。