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 !
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.
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 !
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?
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.