JVM Local Variables Table

Source: Internet
Author: User
Tags garbage collection

Local variables in Java methods are placed in the local variables table of the virtual machine stack:

Java code public static void Main (string[] args) {byte[] waste = new BYTE[6 * 1024 * 1024];        int new_var = 0;    System.GC (); }

public static void Main (string[] args) {
	byte[] waste = new BYTE[6 * 1024 * 1024];
	int new_var = 0;
	System.GC ();
}

The code above is compiled to get:

  Java code   Public static void main (java.lang.string[]);        flags: acc_public, acc_static         code:          stack=1, locals=3, args_size=1             0: ldc             #2                    // int 6291456              2: newarray       byte             4: astore_1              5: iconst_0             6: istore_2              7: invokestatic   #3                    // METHOD JAVA/LANG/SYSTEM.GC: () V            10: return          linenumbertable:            line 3: 0            line 4: 5            line 5: 7           line  6: 10  

public static void Main (java.lang.string[]);
    Flags:acc_public, acc_static
    Code:
      stack=1, locals=3, args_size=1
         0:ldc           #2                  //int 6291456
         2 : NewArray       byte
         4:astore_1
         5:iconst_0
         6:istore_2
         7:invokestatic  #3                  //Method Java /LANG/SYSTEM.GC: () V
        10:return
      linenumbertable: line
        3:0 line 4:5 line 5:7 line
        6:10

You can see locals=3, which means that the local variable table length is 3, with the man function parameters, waste variables and new_var three variables.

In JVM parameters:-verbose:gc-xmx12m-xms12m GC log:

[GC 6514k->6448k (11776K), 0.0011241 secs]

[Full GC 6448k->6356k (11776K), 0.0070312 secs]

This looks normal, and the local variables table references waste and New_var, so their memory is not released.

My question is that before executing System.GC (), the scope of the waste variable has expired, starting with int new_var = 0, and there is no subsequent use of waste, and why the GC cannot reclaim the waste space (although the local variable table holds the waste reference).


In fact, this in the "in-depth understanding of the Java Virtual Machine" has a detailed explanation p202 page.
If you write the method, it will be recycled:
Reference
{
byte B = new BYTE[6 * 1024 * 1024];
}
int a = 0;

So the garbage collector will recycle it right away.
Reason: The slot in the local variable table is reusable, the scope of the variable defined in the method body does not necessarily overwrite the entire method body, and if the value of the current bytecode's PC counter has exceeded the scope of the entire variable, then the corresponding slot of the variable can be used by the other variable.
In contrast to the above code int a = 0; the definition of variable A has exceeded the scope of B, so a will reuse the slot of B (this slot refers to the basic unit of the local variable table), when garbage collection reclaims the space occupied by B.


byte[] Waste = new BYTE[6 * 1024 * 1024];
int new_var = 0;
System.GC ();

When you declare the wate array, then execute the SYSTEM.GC, which is the following code:
byte[] Waste = new BYTE[6 * 1024 * 1024];
System.GC ();

The virtual machine is not recycled waste array, supposedly all virtual machines have quit, why did not clear the waste it.
That's because before the GC is executed, waste is still in the scope of the current main method, and the virtual machine is not daring to recycle waste.

If added int new_var = 0 after waste; This sentence, or did not perform the recovery of memory. Here, a concept is presented:
The Solt slots in a local variable table can be reused in different scope scopes in local methods.
Note that you want to be reusable within different scopes, meaning that the following code is not reusable local variable Solt

byte[] Waste = new BYTE[6 * 1024 * 1024];
int new_var = 0;

And this fragment can be reused for local variables Solt.
{
byte[] Waste = new BYTE[6 * 1024 * 1024];
}
int new_var = 0;
Because waste and new_var are not in the same scope, for the above code, we know that for the static Main method, the local variable table has 3 Solt
Solt0 corresponding string[] parameter
SOLT1 corresponding waste parameters
and New_var corresponding SOLT1, that is solt reuse, of course, waste in the local variable table programming Gcroots unreachable State, this time the implementation of GC
Must have reclaimed the heap memory occupied by waste, instead, for code
byte[] Waste = new BYTE[6 * 1024 * 1024];
int new_var = 0; New_var does not reuse waste's solt, where the local variable table maintains a reference to waste, and GC certainly does not recycle


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.