Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: How to detect current memory free and used in java program ?

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to detect current memory free and used in java program ?

    I would like to know how much of memory is free and used my java program.

    I have used Runtime.totalMemory and freeMemory() functions. However, how come used memory is so different than java.exe process show in windows task manager ?

    Thank !


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How to detect current memory free and used in java program ?

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Windows is probably reporting the total amount of memory used by the JVM (as Java) rather than the memory requirements of your program. I haven't looked at the APIs for the memory methods you've mentioned, because I've never needed to and don't plan to. What are you trying to do, or why do you need to know these things?

  3. #3
    Member jdv's Avatar
    Join Date
    Jul 2014
    Location
    This Land
    Posts
    73
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: How to detect current memory free and used in java program ?

    Quote Originally Posted by tanc08 View Post
    I would like to know how much of memory is free and used my java program.

    I have used Runtime.totalMemory and freeMemory() functions. However, how come used memory is so different than java.exe process show in windows task manager ?
    Correct, because how the memory is used and reported by the VM is only partially related to how the underlying platform specific memory resource allocations work and are reported. Windows Task manager has it's own way of reporting on the total VM address space -- essentially the parent java or javaw process and all of its threads (if any).

    The VM reports on various higher level and, maybe, implementation specific heap statistics it has gathered during the lifetime of the VM.

    These two views will rarely match up except in the loosest sense.

    This is an advanced topic that requires special tools and long experience with _specific_ VM internals and _special_ platform internals to fully grok. Useful, certainly, but unrelated to writing code in Java in the greatest majority of cases.

    You should just make sure to practice good memory housekeeping. If you maintain a larger application that runs for a long time, or crunches bigger data sets, then you will have to learn the tools provided by the VM vendor to query the VM health to ensure you have a reasonably healthy heap churn over time.

Similar Threads

  1. Java program runs out of memory (-xms1024 -xmx1024)
    By Jack Hammered in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 23rd, 2012, 10:29 AM
  2. Replies: 0
    Last Post: October 13th, 2011, 07:05 PM
  3. Replies: 2
    Last Post: June 27th, 2011, 11:14 AM
  4. HELP ME PLEASE MEMORY MANAGEMENT PROGRAM USING JAVA
    By lockwater in forum Java Theory & Questions
    Replies: 1
    Last Post: December 2nd, 2010, 08:30 AM
  5. free memory of bufferedimage
    By mr_empty in forum Java SE APIs
    Replies: 2
    Last Post: January 19th, 2010, 06:14 AM