Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 89065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:39:29+00:00 2026-05-10T22:39:29+00:00

For a particular segment of Java code, I’d like to measure: Execution time (most

  • 0

For a particular segment of Java code, I’d like to measure:

  • Execution time (most likely thread execution time)
  • Memory usage
  • CPU load (specifically attributable to the code segment)

I’m a relative Java novice and am not familiar with how this might be achieved. I’ve been referred to JMX, however I’m not sure how that might be used, and JMX looks a bit ‘heavy’ for what I’m looking to do.

Ideally I’d like some measurement class that can be told what I would like to measure, with the option of calling a start() method prior to a code segment and a stop() method after. Relevant metrics would be logged to a file I specify.

For example:

import com.example.metricLogger;  metricLogger logger = new metricLogger();  logger.setLogPath(pathToLogFile); logger.monitor(executionTime); logger.monitor(memoryUsage); logger.monitor(cpuLoad);  logger.start();  /* Code to be measured */  logger.stop(); 

Is there any standard/common/conventional way of achieving this in Java?

Such measurements are for one-off performance comparisons, and so I’m not looking for any in-production long-term monitoring processes.

I’m more than happy to be referred to tutorials or external examples and don’t expect a full answer here. That said, if anything as simple as the above can be achieved a realistic example would go down really well.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T22:39:30+00:00Added an answer on May 10, 2026 at 10:39 pm

    Profiling may be an easier option since you don’t require in-production stats. Profiling also doesn’t require code modification. VisualVM (which ships w/ the JDK 1.6.06+) is a simple tool. If you want something more in-depth I’d go with Eclipse TPTP, Netbeans profiler, or JProfiler(pay).

    If you want to write you own, consider the following:

    Simple measurments like execution time can be done by ‘clocking’ the section you’re interested in:

    long start = System.nanoTime(); // requires java 1.5 // Segment to monitor double elapsedTimeInSec = (System.nanoTime() - start) * 1.0e-9; 

    You can use a similar technique to monitor memory via Runtime.getRuntime().*memory() methods. Keep in mind that tracking memory usage in a garbage collected environment is trickier than simple subtraction.

    CPU load is hard to measure in Java, I typically stick with execution time and optimize the longer / repetitive sections

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 51k
  • Answers 51k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer When you create a new ASP.NET MVC site, it already… May 11, 2026 at 6:23 am
  • added an answer There are a few major problems with your suggestion: What… May 11, 2026 at 6:23 am
  • added an answer I am not sure if this is the best way… May 11, 2026 at 6:23 am

Top Members

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.