Memory issues with Java virtual machines

Source: Internet
Author: User

Virtual machines account for memory issues
We can use the runtime class to see the state of a virtual machine when a Java program is running.
Each Java application has a runtime class instance that enables the application to connect to the environment in which it is running. The current runtime can be obtained through the GetRuntime method. The application cannot create its own instance of the Runtime class. We need to create it manually. There are three methods in GetRuntime (), as follows:
TotalMemory ()
Returns the total amount of memory in the Java virtual machine. The value returned by this method may change over time, depending on the host environment. Note that the amount of memory required to save an object of any given type may depend on the implementation method. Returns the total amount of memory that is currently provided for the current and subsequent objects, in bytes.
MaxMemory ()
Returns the maximum amount of memory that the Java virtual machine is attempting to use. If the memory itself is not limited, the return value is Long.max_value.
Returns the maximum amount of memory, in bytes, that the virtual machine attempted to use.
Freememory ()
Returns the amount of idle memory in a Java virtual machine. Calling the GC method may result in an increase in the freememory return value.
Returns the approximate total, in bytes, of the currently available memory used by the future allocation object.
Let's look at an example as follows:
Package Runtime;
public class Runtimedemo
{
public static void Main (string[] args) throws Exception
{
Runtime run = Runtime.getruntime ();
SYSTEM.OUT.PRINTLN ("Maximum memory in JVM" + run.maxmemory ());
SYSTEM.OUT.PRINTLN ("Memory used in JVM" + run.totalmemory ());
SYSTEM.OUT.PRINTLN ("Available memory in JVM" + run.freememory ());
String temp = "";
Long start = System.currenttimemillis ();
Use a For loop to constantly consume memory
System.out.println ("Start");
for (int j = 0;j < 10000;j++)
{
temp + = J;
}
System.out.println ("............");
System.out.println ("End");
Long end = System.currenttimemillis ();
SYSTEM.OUT.PRINTLN ("Total cost of executing this program" + (End-start) + "milliseconds");
SYSTEM.OUT.PRINTLN ("Maximum memory in JVM" + run.maxmemory ());
SYSTEM.OUT.PRINTLN ("Memory used in JVM" + run.totalmemory ());
SYSTEM.OUT.PRINTLN ("Available memory in JVM" + run.freememory ());Runtime.getruntime (). GC ();
RUN.GC ();
System.GC ();
Runtime.getruntime (). GC ();
System.out.println ("After garbage removal");
SYSTEM.OUT.PRINTLN ("Maximum memory in JVM" + run.maxmemory ());
SYSTEM.OUT.PRINTLN ("Memory used in JVM" + run.totalmemory ());
SYSTEM.OUT.PRINTLN ("Available memory in JVM" + run.freememory ());}

}

The results of the operation are as follows:

Maximum memory in JVM 1883242496
Memory used in the JVM 128974848
Available memory in JVM 126248272
Start
....................
End
It took 649 milliseconds to complete the program.
Maximum memory in JVM 1883242496
Memory used in the JVM 363331584
Available memory in JVM 138353696
After cleaning up the rubbish
Maximum memory in JVM 1883242496
Memory used in the JVM 363331584
Available memory in JVM 357345560

Memory issues with Java virtual machines

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.