Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
136 views
in Technique[技术] by (71.8m points)

java - How to handle memory Leaks while continuous insertion in a LinkedList?

I have following function which is called continuously from run() of a Thread which is continuously running.

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 allocation Tracker of DDMS says that a lot of objects are created inside addLast() (actually inside addLastImpl() ), So for that I would like to delete the arrays explicitly so that their is always enough memory in Heap. Now,

  1. System.gc() option won't help because it will be called concurrently on every call to setPlayerData().
  2. GC_CONCURRENT is eating all the CPU cycles, since app is very sensitive for time and even a few mili second delay is not acceptable.

For LogCat information please see the link, which is my another question addressing the whole synarion. In this thread I am just trying to solve that bigger problem by dividing it in set of small problems.

A possible solution A possible solution could be to free the memory space explicity by deleting the arrays which are not required. But in Java how can we free an array created by the new operator ? i.e.

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 app GC_CONCURRENT eats up all the time. Other processes are starved due to this. It would have been great if I am able to explicitly free memory, i.e. delete in C++. You can see the LogCat information here...A detailed question regarding my problem

EDIT 2 3. Assign arrays to null

How that will help ? null arrays will be scheduled to Garbage Collection which I want to avoid since the method is called from a thread continuously (in every 20 ms). If I assign the array to null, GC_FOR_MALLOC messages will fill the LogCat...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your problem won't be solved by explicitly deleting objects ... basically because there is no way to do that in Java.

If you are really creating too much garbage for the CMS collector to cope with, then the only solution is to use an object pool to recycle the buffer objects instead of dropping them on the floor for the GC to deal with. However, you need to be careful that you don't replace your current problem with others:

  • The recycled objects may need to be "cleaned" (zeroed).
  • A poorly designed object pool can be a memory leak.
  • A poorly designed object pool can be a concurrency bottleneck.
  • A poorly designed object pool can increase GC overheads, especially if you are running with a heap that is too small.

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 run too close to the limit, the GC won't reclaim much garbage each time. Since the cost of running the GC is proportional to the amount of NON-garbage, it is easy to see that the GC's efficiency is non-linear as the heap gets closer to full.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...