JVM-independent solution to flushing memory

博客介绍了JVM内存清理方法,不依赖垃圾回收器,通过分配和释放内存触发垃圾回收。此方法独立于JVM,但可能对某些JVM造成压力。还提到操作系统分页,当程序过大无法放入物理内存时,会在内存和磁盘分页文件间交换页面,影响程序性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://www.onjava.com/pub/a/onjava/2001/08/22/optimization.html

Ideally, the JVM would provide a method that would flush memory for me. System.gc() looks like it should be such a call. Unfortunately, System.gc() is only a hint to the JVM that "now would be a good time to run the garbage collector." The JVM can ignore the hint, or can run a partial garbage collection, or a full mark-and-sweep of all spaces, or whatever. Instead of relying on the garbage collector, I adapt the method from the previous section to flush memory. I do this by allocating as much memory as possible, as with the earlier testMemory() method, and then I release all held memory and request a little more. The last request is to trigger the garbage collector to immediately reclaim all the memory I was holding onto. The method is straightforward:

public static void flushMemory()
    
{
    
  //Use a vector to hold the memory.
    
  Vector v = new Vector();
    
  int count = 0;
    
  //increment in megabyte chunks initially
    
  int size = 1048576;
    
  //Keep going until we would be requesting 
    
  //chunks of 1 byte
    
  while(size > 1)
    
  {
    
    try
    
    {
    
      for (; true ; count++)
    
      {
    
        //request and hold onto more memory
    
        v.addElement( new byte[size] );
    
      }
    
    }
    
    //If we encounter an OutOfMemoryError, keep 
    
    //trying to get more memory, but asking for 
    
    //chunks half as big.
    
    catch (OutOfMemoryError bounded){size = size/2;}
    
  }
    
  //Now release everything for GC
    
  v = null;
    
  //and ask for a new Vector as a new small object 
    
  //to make sure garbage collection kicks in before 
    
  //we exit the method.
    
  v = new Vector();
    
}
  

This is a JVM-independent solution to flushing memory. You could even conceivably use this in an application if you knew that you had several seconds of time at some point when the application could be doing nothing else, but I wouldn't really recommend it. Although flushMemory() should work on any JVM, it is a stressful procedure and may break some JVMs. In particular I know that the Windows JVM from the Sun 1.2.0 release crashed the main thread when I ran flushMemory(), and put the garbage collector thread into a loop if I additionally ran with the -verbosegc option.



Operating system paging occurs when a program is too big to fit into available real memory (RAM), but can fit in virtual memory. Paging moves pages of the program back and forth between the RAM and the paging file on disk, allowing the operating system to seem like it has a larger memory than the available RAM, but at the expense of program performance.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值