简体   繁体   English

如何跟踪在 Java 应用程序中创建的对象数量?

[英]How to keep track of no of objects created in a java application?

Recently faced this question in an interview:最近在接受采访时遇到了这个问题:

You have a java application, and it uses a connection object to connect to the different clients.您有一个 Java 应用程序,它使用一个连接对象来连接到不同的客户端。 The connection object is of type ClientConnection.连接对象的类型为 ClientConnection。 I want to know how many live connection objects are present at a particular moment in this application?我想知道在此应用程序中的特定时刻存在多少实时连接对象?

Answer given by me:我给出的答案:

  1. I will make a static variable connectionCount in ClientConnection class.我将在 ClientConnection 类中创建一个静态变量 connectionCount。
  2. Every time ClientConnection constructor is called, I will increment the static variable count by 1.每次调用 ClientConnection 构造函数时,我都会将静态变量计数增加 1。
  3. I will override the finalize() method in ClientConnection and decrements the variable count by 1, every time it is called.每次调用时,我将覆盖 ClientConnection 中的 finalize() 方法并将变量计数减 1。

But Interviewer doesn't look satisfied by this answer.但是面试官对这个回答看起来并不满意。
Do we have any other approach for this question?对于这个问题,我们还有其他方法吗?

I think the key part where you went wrong was making the assumption that the interviewer was literally wanting you to track the number of object instances in the JVM.我认为你出错的关键部分是假设面试官实际上希望你跟踪 JVM 中对象实例的数量。 Instead, I think this was meant to be keeping track of the number of objects in an "open" state, where the user of the object would need to dispose of the object via a close() method when they are done using it (similar to how a JDBC Connection works).相反,我认为这是为了跟踪处于“打开”状态的对象数量,在这种情况下,对象的用户在使用完对象后需要通过close()方法处理对象(类似JDBC 连接的工作原理)。

Assuming that ClientConnection has some sort of open and close methods, I would have implemented in the following way:假设ClientConnection有某种打开和关闭方法,我会按以下方式实现:

public class ClientConnection {

  private static final AtomicInteger count = new AtomicInteger();

  public void open() {
    // This could also be in the constructor, depending on
    // the requirements of the object
    count.incrementAndGet();
  }

  public void close() {
    // <close out the physical connection>
    count.decrementAndGet();
  }

  public static int getConnectionCount() {
    return count.get();
  }

}

Basically just using a thread-safe integer as a static member of the class, and then measuring the lifecycle of the connection via open/close methods.基本上只是使用线程安全整数作为类的静态成员,然后通过打开/关闭方法测量连接的生命周期。 You could also consider a public constructor to act as the open method.您还可以考虑将公共构造函数用作open方法。

The finalize() method might not be called immediately when a connection is closed.当连接关闭时,可能不会立即调用 finalize() 方法。 It is not called until the object is garbage collected, which could be at any time, or not at all if the JVM exits without garbage collection being run.在对象被垃圾收集之前不会调用它,这可能在任何时间,或者如果 JVM 退出而没有运行垃圾收集,则根本不会调用。 There should be some kind of listener on the connection that gets called when the connection is closed (when the socket is closed).当连接关闭时(当套接字关闭时),连接上应该有某种侦听器被调用。 Here is some help in solving that problem. 是解决该问题的一些帮助。

Relying on finalize() is bad, because it is not guaranteed to be called within the lifetime of any given program.依赖finalize()是不好的,因为它不能保证在任何给定程序的生命周期内被调用。

Further, just because the JVM garbage collects a given object does not mean the operating system will do anything special with any resources that object requested;此外,仅仅因为 JVM 垃圾收集给定对象并不意味着操作系统会对对象请求的任何资源执行任何特殊操作 for example, assuming ClientConnection opens a socket, the OS might keep the socket alive even after the object has been garbage collected.例如,假设ClientConnection打开一个套接字,即使在对象被垃圾收集之后,操作系统也可能保持套接字处于活动状态。

You need either a listener on the connection or you need to use the object pool pattern.您需要连接上的侦听器,或者需要使用对象池模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM