files in /tmp/hsperfdata_<user>/ were deleted

37 views Asked by At

When I create a file in the /tmp/hsperfdata_/ directory and then run the jps command, the file I just created gets deleted. At what point does the JVM delete the file I created?

I debugged the source code of the jps command and did not find any logic for deleting files. The file has already been deleted at the entry point of the main method of the jps command.

1

There are 1 answers

0
林贻民 On

when creating the vm(Threads::create_vm), global data structure initialization (PerfMemory::perfMemory_init) takes place. During this process, the cleanup_sharedmem_resources method is invoked to clean up outdated files in /tmp/hsperfdata_<user>/.

static void cleanup_sharedmem_resources(const char* dirname) {
  // dirname = "/tmp/hsperfdata_<username>
  DIR* dirp = open_directory_secure_cwd(dirname, &saved_cwd_fd);
  struct dirent* entry;
  errno = 0;
  while ((entry = os::readdir(dirp)) != NULL) {
    pid_t pid = filename_to_pid(entry->d_name);
    if ((pid == os::current_process_id()) || (kill(pid, 0) == OS_ERR && (errno == ESRCH || errno == EPERM))) {
        unlink(entry->d_name);
    }
    errno = 0;
  }
  close_directory_secure_cwd(dirp, saved_cwd_fd);
}