Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Web and WebWorks Development

Reply
Contributor
fasterfene
Posts: 31
Registered: ‎12-05-2011
My Device: No

Re: Device memory is Low issue for an WebWork app

Hi There,

I am also facing this issue on torch 9800.

I am using webwork 2.3 and doing ajax calls to webservice.

It's working properly on Simulator.

After opening the application on actual torch device with in 1-2 min application automatically closes.

I am really fustrated while working wiith webwork application and Client set deadline for this.

I really thankful If I get help from anybody to resolve this issue.

I know this may be memory leak issues but Can somebody sugest me the solution to this problem.

 

 

Thanks,

Sachin

 

 

 

Trusted Contributor
lakshmanan
Posts: 144
Registered: ‎05-04-2011
My Device: blackberry mobiles

Device memory is Low issue for an WebWork app

Tin Neil,

 

Thanks for your reply.. In my device i test this application only.. I deleted all other applications..

 

This is my child browser plugin(Below code)  for blackberry created by using javascript extension... For some URL it works well and for some URL'S  it shows me the low memory message and crashes the application.. I couldnt find where the memory leaks happening..

 

I am also used javascript extension for facebook and twitter connect for my application.. Whenever i login my facebook or twitter account it goes to the home page and immediately it shows me the same low memory message and crashes the application...

 

whenever we open the browser from javascript extension it causes the low memory error..

 

ChildBrowserplugin

 

widgetextension.java

 

import org.w3c.dom.Document;

import net.rim.device.api.browser.field2.BrowserField;
import net.rim.device.api.script.ScriptEngine;
import net.rim.device.api.web.WidgetConfig;
import net.rim.device.api.web.WidgetExtension;
//import net.rim.device.api.system.EventLogger;

public class childbrowserextension implements WidgetExtension {

    public String[] getFeatureList() {
        String[] result = new String[1];
        result[0] = "browser";
        return result;
    }

    public void loadFeature(String feature, String version, Document doc, ScriptEngine scriptEngine) throws Exception
    {
        if (feature == "browser") {
            scriptEngine.addExtension("browser", new childbrowsernamespace());
          
        }
    }

    public void register(WidgetConfig arg0, BrowserField arg1) {
        // TODO Auto-generated method stub

    }

    public void unloadFeatures(Document arg0) {
        // TODO Auto-generated method stub

    }

}

 

childbrowasernamespace.java

 

import net.rim.device.api.ui.component.Dialog;
    import net.rim.device.api.script.Scriptable;
import net.rim.device.api.system.Alert;
import net.rim.device.api.system.Application;

    public class childbrowsernamespace extends Scriptable
    {
        public static final String FIELD_BROWSER = "Open";
        public static final String FIELD_BROWSER_CLOSE = "close";
        public static final String FIELD_BROWSER_GETLOCATION = "getLocation";


        private childbrowserfn _callbrowser;

        public childbrowsernamespace()
        {
            _callbrowser = new childbrowserfn();
        }

        public Object getField(String name) throws Exception
        {
           
           if(name.equals(FIELD_BROWSER))
            {
                return this._callbrowser;
            }
           else if(name.equals(FIELD_BROWSER_CLOSE))
            {
                try
                {
                    synchronized(Application.getEventLock()) {
                        this._callbrowser.browserScreen.close();
                    }
                    return new Boolean(true);
                }
                catch(Exception e)
                {
                    return new Boolean(false);
                }
            }
           else if(name.equals(FIELD_BROWSER_GETLOCATION))
            {


                String loc = this._callbrowser.getLocation();
                //EventLogger.logEvent(ChildBrowserFunction.GUUID_STRING,("getLocation "+loc).toString().getBytes());

                return loc;
            }
            

            return super.getField(name);
        }

    }

childbrowserfunction.java:

 

 

import java.util.Hashtable;

import java.util.Vector;

import net.rim.device.api.script.ScriptableFunction;
import net.rim.device.api.system.Alert;

import java.util.*;
import java.io.*;

import javax.microedition.media.*;


import net.rim.device.api.ui.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;

import com.google.zxing.*;


public final class childbrowserfn extends ScriptableFunction
{
    public browserscreen browserScreen;

    public Object invoke(Object obj, Object[] args) throws Exception
{


try
{
synchronized(Application.getEventLock())
{
String url = (String)args[0];

UiApplication.getUiApplication().pushScreen(new browserscreen(url));
}
}
catch (Exception ex)
{
}
return UNDEFINED;
}

protected Object execute( Object thiz, Object[] args ) throws Exception
{
return invoke(thiz, args);
}

public String getLocation()
{
    return browserScreen.browserfield.getDocumentUrl();
}


}

browserscreen.java

 

import org.w3c.dom.Document;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.system.EventLogger;

import net.rim.device.api.script.ScriptableFunction;

import net.rim.device.api.script.ScriptEngine;
import net.rim.device.api.browser.field.ContentReadEvent;
import net.rim.device.api.browser.field2.BrowserField;
import net.rim.device.api.browser.field2.BrowserFieldListener;
import net.rim.device.api.browser.field2.BrowserFieldConfig;

public class browserscreen extends MainScreen
{
    BrowserField browserfield;
    
    public browserscreen(String url)
    {        
        BrowserFieldConfig config = new BrowserFieldConfig();
        config.setProperty(BrowserFieldConfig.NAVIGATION_MODE, BrowserFieldConfig.NAVIGATION_MODE_CARET);
        
        browserfield = new BrowserField(config);
       
        
        BrowserFieldListener listener = new BrowserFieldListener()
        {
            public void documentLoaded(BrowserField browserField, Document document)
            {
                //EventLogger.logEvent(ChildBrowserFunction.GUUID_STRING, (browserfield.getDocumentUrl() + " doc loaded: " + document.toString()).getBytes());
                try
                {
                    Object[] args = new Object[ 1 ];
                    args[ 0 ] = browserfield.getDocumentUrl();
                    //func.invoke( thiz, args );
                }
                catch(Exception e)
                {
                    //EventLogger.logEvent(ChildBrowserFunction.GUUID_STRING, ("doc loaded err: " + e.toString()).getBytes());
                }
            }
        };
        
        browserfield.addListener(listener);
        browserfield.requestContent(url);
        add(browserfield);
    }
}


Trusted Contributor
lakshmanan
Posts: 144
Registered: ‎05-04-2011
My Device: blackberry mobiles

Re: Device memory is Low issue for an WebWork app

Any suggestions..

Contributor
vipintomer
Posts: 41
Registered: ‎05-03-2012
My Device: 9800 torch

Re: Device memory is Low issue for an WebWork app

Hi Tim,

 

I am developing an WebWork app which calls a lot of html pages and java script function on these pages and display it on the page. The problem we have encountered is that after we have install it on 9800 torch ,after we have used the app to load data for a few times, the app will say "Device Memory is Low" and request to close some opened application...... On opened my application only and we have only used it for while, and after a few more click, the system will force the app to quit. Please help me out of this . Please suggest if there any professional help for the same. I have applied memory cleaning enable on device and increase the size of cache used in app by setting it in config file . Even when i take 2 or 3 html pages with 4-5 images and navigate between tham i got the same error. My cod file is of size 1.2MB and it takes upto 40 MB at run time and going over and gets close by OS itself forcefully. Can we clear cache between html pages . I have used localstorage.clean(); on html page as well but no sol. Thanks in advance for any help in this. 

 

for the above issue can you please let me know that i can fix it or not if yes than what possible ways to do it?

 

Vipin

 

 

Trusted Contributor
lakshmanan
Posts: 144
Registered: ‎05-04-2011
My Device: blackberry mobiles

Re: Device memory is Low issue for an WebWork app

Hello Friends,

 

Rim guys this is very urgent issue to be solved for our application. I provided example of javascript extension problem in my previous post. please analyse it and provide us any solution..

 

Regards,

Lakshmanan

 

Visitor
RageBar
Posts: 1
Registered: ‎05-15-2012
My Device: Developer

Re: Device memory is Low issue for an WebWork app


lakshmanan wrote:

Hello Friends,

 

Rim guys this is very urgent issue to be solved for our application. I provided example of javascript extension problem in my previous post. please analyse it and provide us any solution..

 

Regards,

Lakshmanan

 


Not to depress you or anything, but if you look at the fact that this thread was opened in June of last year, and that it's now 25 pages long of people complaining about the same issue, and getting only crappy work-arounds recommended to "mitigate" (not fix) the issue... I doubt you will get any helpful answer.

Contributor
vipintomer
Posts: 41
Registered: ‎05-03-2012
My Device: 9800 torch

Re: Device memory is Low issue for an WebWork app

Hey everyone please follow these steps to remove the memory low issue on Blackberry

 

Put these file under
eclipse\plugins\net.rim.

browser.tools.wcpc_2.0.0.201103231828-18\wcpc\device_templates\blackberry\web\widget
Add this MemoryMaid.java under widget foler--
if you are not using plugin under eclipse than try
package blackberry.web.widget;

import blackberry.web.widget.device.DeviceInfo;
import net.rim.device.api.system.Memory;

/**
 * Helps force garbage collection in case the application runs low on memory
 */
public class MemoryMaid extends Thread {
    protected final static int LOWMEM_THRESHHOLD = 1024 * 1024 * 10; // 5Mb--3
    protected final static float DEVIATION_THRESHHOLD = 0.10f; // 10%
    protected final static long GC_TIMEOUT = 20000; //25 sec Wait time after a gc. To avoid calling gc too much15sec
    protected final static long SAMPLE_RATE = 5000; //5 sec How often to check memory--4

    protected static MemoryMaid _instance = null;

    private boolean _running;
    private boolean _doGC;
    private int _lastSampleAfterGC;

    private MemoryMaid() {

    }

    /**
     * @return A singleton instance of the MemoryMaid class
     */
    public static MemoryMaid getInstance() {
        if( true ) {
            if( _instance == null ) {
                _instance = new MemoryMaid();
				 _instance.startGC();
            }
            return _instance;
        }
        return null;
    }

   
    public void run() {
        _running = true;

        _lastSampleAfterGC = Memory.getRAMStats().getFree();

        // Avoid handling low memory if the device is low before we start.
        if( _lastSampleAfterGC < LOWMEM_THRESHHOLD ) {
            _running = false;
        }

        while( _running ) {
            // Don't waste time if we haven't done anything that may have triggered the memory drop
            if( true){//_doGC ) {
                //_doGC = false;
                // Clean memory if only LOWMEM_THRESHHOLD is left or if free memory changes by a DEVIATION_THRESHHOLD percentage
                int currentSample = Memory.getRAMStats().getFree();
                if( currentSample < LOWMEM_THRESHHOLD || currentSample / _lastSampleAfterGC <= 1 - DEVIATION_THRESHHOLD ) {
                    System.gc();
                    _lastSampleAfterGC = Memory.getRAMStats().getFree();
                    if( _running ) {
                        try {
                            Thread.sleep( GC_TIMEOUT );
                        } catch( InterruptedException e ) {
                            _running = false;
                        }
                    }
                }
            }

            if( _running ) {
                try {
                    Thread.sleep( SAMPLE_RATE );
                } catch( InterruptedException e ) {
                    _running = false;
                }
            }
        }
        _running = false;
    }

    /**
     * Stop the thread
     */
    public void stop() {
        _lastSampleAfterGC = -1;
        _running = false;
        this.interrupt();
    }

	//vipin added this
	 public void startGC() {
        
        this.start();
    }


    /**
     * The memory maid will only do a GC if this flag is set and the appropriate wait time has passed
     */
    public void flagGC() {
        _doGC = true;
    }

	
}

 


find out device template folder under Blackberry SDK
and place these classes under device template foler.
/*
* Copyright 2010-2011 Research In Motion Limited.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package blackberry.web.widget;

import net.rim.device.api.system.Characters;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.web.WidgetConfig;

import blackberry.core.ApplicationEventHandler;
import blackberry.core.EventService;
import blackberry.web.widget.listener.HardwareKeyListener;

/**
 * Just a pass through class - provides flexibility with future screen types (I hope).
 */
public abstract class WidgetScreen extends MainScreen {
    protected HardwareKeyListener _keyListener;
    protected WidgetConfig _wConfig;
    
    protected WidgetScreen() {
        super();
    }

    protected WidgetScreen( long style ) {
        super( style );
    }
    
    protected WidgetScreen( Widget widget, long style ) {
        super( style );
        _keyListener = widget.getHardwareKeyListener();
        _wConfig = widget.getConfig();
    }
    
    public boolean onClose() {        
        if( EventService.getInstance().fireEvent(ApplicationEventHandler.EVT_APP_EXIT, null, true) ) {
            return false;
        }
        System.gc(); // MemoryMaid
        // Do not call the default onClose function so the save dialog is skipped.
        close();
        return true;
    }
    
    public void close() {
        // fire an unconsumable EXT event
        EventService.getInstance().fireEvent(ApplicationEventHandler.EVT_APP_EXIT, null, false);
        super.close();
    }
    
    /**
     * Handle the escape button if it was not previously handled.
     */
    protected boolean keyCharUnhandled( char key, int status, int time ) {
        // Fire previously stored key handling events
        if( _keyListener.isActive() ) {
            if (_keyListener.fireStoredEvent()) {
                return true;
            }
        }
        
        // Specific handling for 'back' button
        if( key != Characters.ESCAPE ) {
            return super.keyCharUnhandled( key, status, time );
        } else {
            return onBackButton();
        }
    }
    
    protected abstract boolean onBackButton();
}

 


And replace the widget screen class by this one


Thanks
Contributor
vipintomer
Posts: 41
Registered: ‎05-03-2012
My Device: 9800 torch

Re: Device memory is Low issue for an WebWork app

I have fixed this issue and i can help you to out of this issue.

 

By doing changes in Blackberry SDK.

 

Vipin Tomer

9582215858

New Contributor
mbelluco
Posts: 4
Registered: ‎04-06-2012
My Device: BlackBerry Curve 9300

Re: Device memory is Low issue for an WebWork app

Hi we have checked your solutions for the memory low issue, but in our app there is still this problem... any other suggestion?

 

Thanks

Contributor
vipintomer
Posts: 41
Registered: ‎05-03-2012
My Device: 9800 torch

Re: Device memory is Low issue for an WebWork app

Is the GC started to work .

 

Or it does't improve your app completely ,

 

Than you should go with native app.