×

Discussion Board

Results 1 to 8 of 8
  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    10

    Memory issue with multithreading in J2ME

    Hi all,

    I am writing a small module which having a seprated thread downloading content from the internet then fire an event to update the UI. When optimizing the memory usage with using the Memory monitor Extension(in WTK 2.2.5 from Sun), I found following issue that an object is created in the main thread could not 100% release if it is passed into a thread worker.
    Here are the steps to reproduce:

    1. Potential Memory leak error case:
    // Create a http request to start a session, the request object have a method do sending login data to server in retrieving session.
    NetworkSessionStartRequest request = new NetworkSessionStartRequest("thanhngocdao", "123456", null);
    // Add this request to queue. RequestWork object will get request from queue then execute one by one.
    // The request will be removed from queue when it is executed.
    RequesThreadtWorker.getInstance().addAPIRequest(request);
    // Point the object variable to null so that GC can free it.
    request = null;
    => When profiling the application with the above code running, after the request is finished and all pointers to the request object have been set to null. I found it still take some space in memory. which caused by the javax.microedition.io.Connector.open still take some bytes in memory.

    The following are case with no memory error:

    2. No memory error found:

    NetworkSessionStartRequest request = new NetworkSessionStartRequest("thanhngocdao", "123456", null);
    // Invoke the runRequest from the main thread.
    request.RunRequest();
    // Point the object variable to null so that GC can free it.
    request = null;

    => In this case, there only one main thread, after the code running, all data were released.

    Can anyone help to explain, why even I did set all pointers to the object to null, it still could not be released properly when using multithreading but work fine without multithreading?

    Thank you!

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Memory issue with multithreading in J2ME

    Hard to know without knowing more of the code.

    First thing that comes to mind: once your create and start() a Thread, it can't become garbage until it completes. Therefore, nor can any object referenced from that thread.

    Graham.

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    10

    Re: Memory issue with multithreading in J2ME

    Quote Originally Posted by grahamhughes View Post
    Hard to know without knowing more of the code.

    First thing that comes to mind: once your create and start() a Thread, it can't become garbage until it completes. Therefore, nor can any object referenced from that thread.

    Graham.
    Hi Graham,

    Thank you lot for your advices. Actually the thread was competed. The stranger thing I found thats even the local StringBuffer object could not be released after the method was executed.
    Here is the code:

    public class RequestThreadWorker
    implements Runnable
    {

    private static RequestThreadWorker mInstance = null;
    private Vector mRequestQueue;
    private Thread mThread;

    private ZingMeAPIRequestWorker()
    {
    mRequestQueue = new Vector();
    mThread = null;
    }

    public static ZingMeAPIRequestWorker getInstance()
    {
    if(mInstance == null)
    {
    mInstance = new ZingMeAPIRequestWorker();
    }
    return mInstance;
    }

    public void addAPIRequest(APIRequest request)
    {
    if(mThread == null)
    {
    mRequestQueue.addElement(request);
    mThread = new Thread(this);
    mThread.start();
    }
    else
    {
    synchronized(this)
    {
    mRequestQueue.addElement(request);
    }
    }
    }

    public void clearAllRequests()
    {
    synchronized(this)
    {
    for(int i = 0; i < mRequestQueue.size(); i++)
    {
    APIRequest request = (APIRequest)mRequestQueue.elementAt(i);
    request.cancelRequest();
    }

    mRequestQueue.removeAllElements();
    }
    }

    public void run()
    {
    while(true)
    {
    APIRequest request = null;
    synchronized(this)
    {
    if(mRequestQueue.size() > 0)
    {
    request = (APIRequest)mRequestQueue.firstElement();
    mRequestQueue.removeElementAt(0);
    } else
    {
    mThread.interrupt();
    mThread = null;
    }
    }
    if(request != null)
    {
    request.runRequest();
    }
    else
    { return;}
    request = null;
    System.gc();
    }
    }
    }


    class NetworkSessionStartRequest:
    {
    String mServerUrl;
    Hashtable mParams

    public NetworkSessionStartRequest(String username, String password, IRequestListener listener)
    {
    mServerUrl = "http://....";
    mParams = new Hashtable();
    mParams.put("username", username);
    mParams.put("password", password);
    }

    protected int runRequest()
    {
    mData = null;
    System.gc();


    byte result[];


    result = null;

    StringBuffer multipartBuffer = new StringBuffer("");
    StringBuffer httpGet = new StringBuffer("");

    for (Enumeration keys = params.keys(); keys.hasMoreElements(); multipartBuffer.append("\r\n")) {
    String key = (String) keys.nextElement();

    httpGet.append(key + "=" + StringUtils.encodeString(value) + "&");
    }
    httpGet = httpGet.deleteCharAt(httpGet.length() - 1);

    try
    {
    String output = "";

    HttpHandler web = new HttpHandler();
    result = web.requestGet(mServerUrl + httpGet.toString());

    if(result.length > 0)
    {
    output = new String(result);
    Logger.println(output);
    }
    mData = result;
    web.closeConnection;
    web = null;

    } catch (Exception e) {
    Logger.printErrorln("APIRequest.callMethod(): Could not retrieve output data." + e.getMessage());

    return -1;
    }
    return 0;
    }
    }
    Last edited by vngwcc; 2010-06-27 at 17:13.

  4. #4
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Memory issue with multithreading in J2ME

    Nothing obviously wrong. Does the code actually run out of memory? Could you just be seeng phantoms in the memory monitor?

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    10

    Re: Memory issue with multithreading in J2ME

    Quote Originally Posted by grahamhughes View Post
    Nothing obviously wrong. Does the code actually run out of memory? Could you just be seeng phantoms in the memory monitor?
    Actually the code run out of memory on S40 device with heapsize 2mb, when I downloaded to about 30 pictures with 16 bit(50 x 50 x 2 = 5k * 30), I am trying to put these images into storage using recordstore and also do profiling to find out memory leak errors caused by incorrect object references. For the code below, if I removed the StringBuffer, Connection.open(...) from the modules then I did not see the RequestThreadWorker.run() method live in memory anymore. But if I put it back, then i found it still alive and take some amount of memory, using Memory monitor.
    If there maybe something wrong with the Memory monitor tool from Sun's WTK, please suggest a good profiler tool .

    Thank you!

    -Thanh

  6. #6
    Super Contributor
    Join Date
    Apr 2007
    Posts
    2,708

    Re: Memory issue with multithreading in J2ME

    just an idea, in ur NetworkSessionStartRequest class you might want to add a finally at your try/catch, in which you can close all StringBuffer items...
    Also is it possible you are using some inputStreams or outputStreams somewher in your networking code that are not closed/garbaged ?
    Note: please don't send private questions to me, as I don't have time to provide private support. All such questions should be posted on public forums, where I and others can see and answer them.

  7. #7
    Registered User
    Join Date
    Jun 2010
    Posts
    10

    Re: Memory issue with multithreading in J2ME

    Quote Originally Posted by Tiger79 View Post
    just an idea, in ur NetworkSessionStartRequest class you might want to add a finally at your try/catch, in which you can close all StringBuffer items...
    Also is it possible you are using some inputStreams or outputStreams somewher in your networking code that are not closed/garbaged ?
    Thanks Tiger79,

    I had closed all in/out stream, connection in the web.closeConnection() method and looks like there was no exception occured during the test. In a simplest version, I only put a simple line "StringBuffer testString = new StringBuffer(); testString = null;" in the NetworkSessionStartRequest.runRequest() method but still see the object data live after the thread was completed. In case I invoke the NetworkSessionStartRequest.runRequest() from the main thread then seem things are ok, after its finished there was nothing live in memory. The problem only occured with having it run in another Thread.
    I am trying understand the behavior of the Sun WTK memory monitor tool and also find another profiler tool which detailly allow to see which object is live. If you know any, pls suggest.

  8. #8
    Super Contributor
    Join Date
    Apr 2007
    Posts
    2,708

    Re: Memory issue with multithreading in J2ME

    well don't take the emulator's data and figures as real-world on-device figures !
    The emulator's memory monitor is very handy to see a emulated behaviour on a device, it also can give you a rough idea on what objects will take up most of the memory but it is NOT a precise tool which will precisely tell you how the memory usage will be on a real device...
    My personal advice would be : get a hold of a S60 device, you can do on-device debugging on those, and the memory monitor works on them too whilst debugging ! Have a look if you have the same memory issue on the device itself, if not then it might be a problem of the emulator, but nothing you should worry on real devices...
    Note: please don't send private questions to me, as I don't have time to provide private support. All such questions should be posted on public forums, where I and others can see and answer them.

Similar Threads

  1. Memory issue
    By uzair_w in forum Feedback - Nokia Developer Services & Infrastructure
    Replies: 3
    Last Post: 2009-11-14, 10:57
  2. Memory Issue
    By zanvi in forum Symbian
    Replies: 8
    Last Post: 2009-04-28, 09:17
  3. Problem in multithreading while trying to connect server using SOCKET(J2ME Apps)
    By rijwan1234 in forum Mobile Java Networking & Messaging & Security
    Replies: 7
    Last Post: 2008-05-22, 03:30
  4. A Memory Issue
    By ziyaaf in forum Mobile Java General
    Replies: 2
    Last Post: 2008-03-13, 17:54
  5. Multithreading issue in device drivers
    By SymbianDev10 in forum Symbian
    Replies: 1
    Last Post: 2007-09-04, 05:11

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×