How to handle Leak memory while continuously pasting in LinkedList?

I have the following function that is continuously called from run () of a thread that runs continuously.

private LinkedList<short[]> playerData = new LinkedList<short[]>(); public synchronized void setPlayerData(short[] buffer) { // Log.i(LOG_TAG, "Inside setData.."); playerData.addLast(buffer); if (playerData.size() > 10) { // Log.i(LOG_TAG, "playerData not empty"); playerData.removeFirst(); } } 

Now the Tracker DDMS distribution says that many objects were created inside addLast () (actually inside addLastImpl ()), so for this I would like to explicitly delete arrays so that they always have enough memory on the heap. Now

  • System.gc () will not help, because it will be called at the same time each time setPlayerData () is called.
  • GC_CONCURRENT eats all processor cycles, because the application is very time sensitive, and even a small delay in the second second is unacceptable.

For information about LogCat, see the section, which is my other question regarding the entire synarion. In this thread, I'm just trying to solve this big problem by dividing it into many small problems.

Possible solution . A possible solution would be to free up memory space exploit by removing arrays that are not required. But in Java, how can we free the array created by the new operator? those.

 short[] buffer = new short[320]; // do some operation on buffer /// now how can I explicitly free memory by deleting the buffer, when its job is over.. 

I know there is garbage collection to take care of all such things. But in my application, GC_CONCURRENT eats all the time. Because of this, because of this, other processes disappear. It would be great if I could explicitly free the memory, i.e. Delete in C ++. Here you can see the LogCat information ... A detailed question about my problem

EDIT 2 3. Assign arrays to zero

How does this help? null arrays will be assigned to the garbage collection, which I want to avoid, since the method is called from the stream continuously (every 20 ms). If I set the array to zero, GC_FOR_MALLOC messages will populate LogCat ...

-2
source share
5 answers

Your problem will not be solved by explicitly deleting objects ... mainly because in Java there is no way to do this.

If you really create too much garbage for the CMS collector to handle, then the only solution is to use the object pool to recycle buffer objects instead of throwing them on the floor for GC. However, you need to be careful that you do not replace your current problem with others:

  • Secondary objects can be "cleared" (zeroed out).
  • Poor object pool design can be a memory leak.
  • An invalid object pool may be a concurrency bottleneck.
  • An underdeveloped pool of objects can increase GC overhead, especially if you work with too little heap.

On the other hand, your real problem may be that your heap is too small for the application you are trying to run. If you go too close to the limit, the GC will not return a lot of garbage every time. Since the cost of starting a GC is proportional to the amount of NON garbage, it is easy to see that the efficiency of the GC is non-linear, as the heap gets closer to full.

+6
source

Just set your objects to NULL after use, and the garbage collector will automatically take care of this. In practice, the Garbage Collector starts when your objects are inaccessible by code, so it destroys them to free up space.

+4
source

There is no delete operator in Java. The garbage collector automatically destroys the object when parts of the code no longer use them.

That is, when an object becomes inaccessible, the garbage collector destroys this object and frees memory.

I read the editing, maybe you can solve your problem with the buffer pool []. Therefore, the garbage collector does not need to constantly garbage buffers.

private LinkedList playerData = new LinkedList ();

 public synchronized void setPlayerData(short[] buffer) { // Log.i(LOG_TAG, "Inside setData.."); playerData.addLast(buffer); if (playerData.size() > 10) {  // Log.i(LOG_TAG, "playerData not empty");  byte[] buffer = playerData.removeFirst(); // here return the buffer to the pool to reuse it } 

}

+2
source

If you have problems with the garbage collector, it is best to allocate less new memory. One way to do this is to use object pools . Basically, reuse a buffer that you no longer need, instead of creating a new one.

+1
source

There is no โ€œequivalentโ€ operator in Java. The system collects garbage in the background. No explicit method call guarantees immediate release of memory.

+1
source

All Articles