Understanding Java Memory Management

2019-01-13 01:51发布

Java programmers know that JVM runs a Garbage Collector, and System.gc() would just be a suggestion to JVM to run a Garbage Collector. It is not necessarily that if we use System.gc(), it would immediately run the GC.

Please correct me if I misunderstand Java's Garbage Collector.

Is/are there any other way/s doing memory management other than relying on Java's Garbage Collector?
If you intend to answer the question by some sort of programming practice that would help managing the memory, please do so.

10条回答
The star\"
2楼-- · 2019-01-13 02:15

Well, the GC is always there -- you can't create objects that are outside its grasp (unless you use native calls or allocate a direct byte buffer, but in the latter case you don't really have an object, just a bunch of bytes). That said, it's definitely possible to circumvent the GC by reusing objects. For instance, if you need a bunch of ArrayList objects, you could just create each one as you need it and let the GC handle memory management; or you could call list.clear() on each one after you finish with it, and put it onto some queue where somebody else can use it.

Standard best practices are to not do that sort of reuse unless you have good reason to (ie, you've profiled and seen that the allocations + GC are a problem, and that reusing objects fixes that problem). It leads to more complicated code, and if you get it wrong it can actually make the GC's job harder (because of how the GC tracks objects).

查看更多
Animai°情兽
3楼-- · 2019-01-13 02:16

Basically the idea in Java is that you should not deal with memory except using "new" to allocate new objects and ensure that there is no references left to objects when you are done with them.

All the rest is deliberately left to the Java Runtime and is - also deliberately - defined as vaguely as possible to allow the JVM designers the most freedom in doing so efficiently.

To use an analogy: Your operating system manages named areas of harddisk space (called "files") for you. Including deleting and reusing areas you do not want to use any more. You do not circumvent that mechanism but leave it to the operating system

You should focus on writing clear, simple code and ensure that your objects are properly done with. This will give the JVM the best possible working conditions.

查看更多
爷的心禁止访问
4楼-- · 2019-01-13 02:18

Below is little summary I wrote back in the days (I stole it from some blog, but I can't remember where from - so no reference, sorry)

  1. There is no manual way of doing garbage collection in Java.
  2. Java Heap is divided into three generation for the sake of garbage collection. These are the young generation, tenured or old generation, and Perm area.
  3. New objects are created in the young generation and subsequently moved to the old generation.
  4. String pool is created in Perm area of Heap, Garbage collection can occur in perm space but depends on upon JVM to JVM.
  5. Minor garbage collection is used to move an object from Eden space to Survivor 1 and Survivor 2 space, and Major collection is used to move an object from young to tenured generation.
  6. Whenever Major garbage collection occurs application, threads stops during that period which will reduce application’s performance and throughput.
  7. There are few performance improvements has been applied in garbage collection in Java 6 and we usually use JRE 1.6.20 for running our application.
  8. JVM command line options -Xms and -Xmx is used to setup starting and max size for Java Heap. The ideal ratio of this parameter is either 1:1 or 1:1.5 based on my experience, for example, you can have either both –Xmx and –Xms as 1GB or –Xms 1.2 GB and 1.8 GB.

Command line options: -Xms:<min size> -Xmx:<max size>

查看更多
爱情/是我丢掉的垃圾
5楼-- · 2019-01-13 02:22

The most important thing to remember about Java memory management is "nullify" your reference.

Only objects that are not referenced are to be garbage collected.

For example, objects in the following code is never get collected and your memory will be full just to do nothing.

List objs = new ArrayList();
for (int i = 0; i  < Integer.MAX_VALUE; i++) objs.add(new Object());

But if you don't reference those object ... you can loop as much as you like without memory problem.

List objs = new ArrayList();
for (int i = 0; i  < Integer.MAX_VALUE; i++) new Object();

So what ever you do, make sure you remove reference to object to no longer used (set reference to null or clear collection).

When the garbage collector will run is best left to JVM to decide. Well unless your program is about to start doing things that use a lot of memory and is speed critical so you may suggest JVM to run GC before going in as you may likely get the garbaged collected and extra memory to go on. Other wise, I personally see no reason to run System.gc().

Hope this help.

查看更多
霸刀☆藐视天下
6楼-- · 2019-01-13 02:32

"Nullify"ing the reference when not required is the best way to make an object eligible for Garbage collection.

There are 4 ways in which an object can be Garbage collected. 1. point the reference to null, once it is no longer required.

String s = new String("Java");

Once this String is not required, you can point it to null.

s = null;

Hence, s will be eligible for Garbage collection.

  1. point one object to another, so that both reference points to same object and one of the object is eligible for GC.

    String s1 = new String("Java");

    String s2 = new String("C++");

In future if s2 also needs to pointed to s1 then;

s1 = s2;

Then the object having "Java" will be eligible for GC.

  1. All the objects created within a method are eligible for GC once the method is completed. Hence, once the method is destroyed from the stack of the thread then the corresponding objects in that method will be destroyed.

  2. Island of Isolation is another concept where the objects with internal links and no extrinsic link to reference is eligible for Garbage collection. "Island of isolation" of Garbage Collection

Examples: Below is a method of Camera class in android. See how the developer has pointed mCameraSource to null once it is not required. This is expert level code.

public void release() {
        if (mCameraSource != null) {
            mCameraSource.release();
            mCameraSource = null;
        }
    }

How Garbage Collector works?
Garbage collection is performed by the daemon thread called Garbage Collector. When there is sufficient memory available that time this demon thread has low priority and it runs in background. But when JVM finds that the heap is full and JVM wants to reclaim some memory then it increases the priority of Garbage collector thread and calls Runtime.getRuntime.gc() method which searches for all the objects which are not having reference or null reference and destroys those objects.

查看更多
该账号已被封号
7楼-- · 2019-01-13 02:34

Just to add to the discussion: Garbage Collection is not the only form of Memory Management in Java.

In the past, there have been efforts to avoid the GC in Java when implementing the memory management (see Real-time Specification for Java (RTSJ)). These efforts were mainly dedicated to real-time and embedded programming in Java for which GC was not suitable - due to performance overhead or GC-introduced latency.

The RTSJ characteristics

  • Immortal and Scoped Memory Management - see below for examples.
  • GC and Immortal/Scoped Memory can coexist withing one application
  • RTSJ requires a specially modified JVM.

RTSJ advantages:

  • low latency, no GC pauses
  • delivers predictable performance that is able to meet real-time system requirements

Why RTSJ failed/Did not make a big impact:

  • Scoped Memory concept is hard to program with, error-prone and difficult to learn.
  • Advance in Real-time GC algoritms reduced the GC pause-time in such way that Real-time GCs replaced the RTSJ in most of the real-time apps. However, Scoped Memories are still used in places where no latencies are tolerated.

Scoped Memory Code Example (take from An Example of Scoped Memory Usage):

import javax.realtime.*;
public class ScopedMemoryExample{

    private LTMemory myMem;

    public ScopedMemoryExample(int Size) {

       // initialize memory
       myMem = new LTMemory(1000, 5000); 
    }

public void periodicTask() {

  while (true)) {
    myMem.enter(new Runnable() {
        public void run() {
          // do some work in the SCOPED MEMORY
          new Object();
          ...
          // end of the enter() method, the scoped Memory is emptied.  
        }
    });
  }


}
}

Here, a ScopedMemory implementation called LTMemory is preallocated. Then a thread enters the scoped memory, allocates the temporary data that are needed only during the time of the computation. After the end of the computation, the thread leaves the scoped memory which immediately makes the whole content of the specific ScopedMemory to be emptied. No latency introduced, done in constant time e.g. predictable time, no GC is triggered.

查看更多
登录 后发表回答