Memory usage of a GWT application - gwt

I'm currently working on a GWT application as a proof of technology for future projects. I like the way of building my AJAX code in Java instead of JavaScript. But I seem to be running into a memory problem when I repeat calls to an RPC service. The browser memory usage keeps growing and growing.
When searching Google I keep reading about how great GWT is and that its impossible to get memory leaks so can anyone explain why my browser (Firefox and Chromium) memory is rocketing?
Thanks in advance for helping me,
Bram
The code:
...
class ExampleTable extends Composite
private RPCService rpcService;
private Timer tableUpdater;
public ExampleTable(){
... Init timer and RPC Service
... Add components
initWidget();
}
private void getTableDataFromRPCService() {
this.rpcService.getData(new AsyncCallback<ArrayList<Data>>() {
#Override
public void onSuccess(ArrayList<Data> result) {
ExampleTable.this.updateTable(result);
}
#Override
public void onFailure(Throwable caught) {
//Do nothing
}
});
}
private void updateTable(ArrayList<Data> tableData){
... Update the table
}
private void startUpdateTask() {
this.serviceUpdater = new Timer() {
#Override
public void run() {
ExampleTable.this.getTableDataFromRPCService();
}
};
serviceUpdater.scheduleRepeating(2000);
}
}
EDIT:
I've spent some time to write a test application which can be downloaded here. I ran the application for about half an hour with the table update enabled after that Firefox took about 350MB of memory. I also ran the test with the update table disabled for an hour memory usage in Firefox went to little over 100MB.
(To run this sample you need the Google visualization API for GWT which can be downloaded from Google but I'm not allowed to post the link because of a new user policy )
I just got home from work and start another test without the table data update to see if memory usage keeps increasing or if it stops at a certain point.
This is the client implementation class (GWTMemoryIssue.java):
public class GWTMemoryIssue implements EntryPoint {
//Run with or without table
private static final boolean WITH_TABLE = false;
private final TestServiceAsync rpcService = GWT.create(TestService.class);
private Panel panel;
private Timer timer;
private Table table;
public void onModuleLoad() {
RootPanel rootPanel = RootPanel.get();
this.panel = new VerticalPanel();
this.panel.setSize("100%", "100%");
rootPanel.add(panel);
if (WITH_TABLE) {
loadTable();
}else{
startUpdateTask();
}
}
private void startUpdateTask() {
this.timer = new Timer() {
#Override
public void run() {
GWTMemoryIssue.this.getTableData();
}
};
this.timer.scheduleRepeating(2000);
}
public void loadTable() {
Runnable onLoadCallback = new Runnable() {
public void run() {
GWTMemoryIssue.this.table = new Table(createTableData(), createTableOptions());
GWTMemoryIssue.this.table.setSize("100%", "100%");
GWTMemoryIssue.this.panel.add(GWTMemoryIssue.this.table);
GWTMemoryIssue.this.startUpdateTask();
}
};
VisualizationUtils.loadVisualizationApi(onLoadCallback, Table.PACKAGE);
}
private Options createTableOptions() {
Options options = Options.create();
return options;
}
private DataTable createTableData() {
DataTable data = DataTable.create();
data.addColumn(ColumnType.STRING, "Name");
data.addColumn(ColumnType.NUMBER, "Intval 1");
data.addColumn(ColumnType.NUMBER, "Intval 2");
data.addColumn(ColumnType.NUMBER, "Intval 3");
return data;
}
private void getTableData() {
rpcService.getListOfItems(new AsyncCallback<ArrayList<ListItem>>(){
public void onFailure(Throwable caught) {
// Do nothing
}
public void onSuccess(ArrayList<ListItem> result) {
if (WITH_TABLE){
GWTMemoryIssue.this.updateTableData(result);
}else{
//Ignore the data from the server
}
}
});
}
private void updateTableData(ArrayList<ListItem> result) {
DataTable data = createTableData();
data.addRows(result.size());
int row = 0;
for (ListItem li : result) {
data.setValue(row, 0, li.getName());
data.setValue(row, 1, li.getIntVal());
data.setValue(row, 2, li.getIntSecondVal());
data.setValue(row, 3, li.getThirdIntVal());
row++;
}
this.table.draw(data, createTableOptions());
}
}

With all the additional information you provided here are some thoughts. I guess the memory increase is caused by the lists remaining in memory. It's either possible the memory is not freed at all or the JavaScript garbage collector doesn't get time to clean up, due too the short time frame between updates. Here are some test you could do:
To test if the garbage collector doesn't get time adapt you code such that the update only runs a finite number of times, and then check if the memory usage decreases over a few minutes. If the memory usage decreases, than it might be less of an issue in you real world example. But simple test it by setting the delay to the 30 seconds.
You could help the garbage collector by clearing the list after they are used: I don't know what works best, so here are some suggestions: Remove object from the list, or loop over the list and set values to null. This should not be necessary in normal case, because the garbage collector would do it.
You can also try the following Firefox Memory profiler add-on to see if you can locate the memory increases: http://ajaxian.com/archives/enhanced-firefox-memory-profiler-add-on

I've been using GWT quite some time with many tables and RPCs and until now most memory leaks I found were my own fault.
The RPC layer does not seem to leak as far as I know and your example is just too simple to pose a problem.
You might need to take a look at what the updateTable method is actually doing, you might have a leak in your own code.
One thing that can cause huge memory leaks with GWT are Imagebundles in IE. It is a known fact that these leak extremely in GWT because they are using DXTransform to support alpha transparency. The memory goes up in large chunks everytime widgets are put on the screen. But there are tricks to avoid this.

No there is no explicit operation needed to clean garbage in Javascript. It is supposed to run automatically (although the GC in the browser is not on the same level as in a modern JVM).
GWT does it best to avoid common pitfalls that would cause memory leaks in JS (circular references between JS and DOM nodes are badly handled in some browsers).
So the question is: is the memory usage always going up ? Or does it top out at a certain point (or it just crashes with some out of memory ?). It can be normal that your application seems to be growing and growing... but GC should kick in at some point.
In my application memory usage tops out at about 64MB. But I am not working on Ubuntu, IE on windows is our main target, although I sometimes test on FireFox as wel (and there I don't see a leak either).
One other thing you might need to do is to avoid polling every 2 seconds like you do. If a request takes longer than 2 secs you start queing up the requests (a browser has a limitation on the number of concurrent connections). so it's best to wait for the response before firing a new timer.

Related

Unity game stops while coroutine running

Unity(character movement and everything in current game scene) stops while downloading textures from web url.
I'm using Socket.IO for online multiplayer.
request 'map info' with socket.emit in void Start()
get 'map info' and create map entities with Instantiate()
get profile images(textures) from url and change texture of map entities
IEnumerator DownloadImage(string MediaUrl, SpriteRenderer spr) {
// check if url is malformed
if (MediaUrl == null || MediaUrl.Contains("Null")) {
yield break;
}
UnityWebRequest request = UnityWebRequestTexture.GetTexture(MediaUrl);
yield return request.SendWebRequest();
if (request.result == UnityWebRequest.Result.ConnectionError || request.result == UnityWebRequest.Result.DataProcessingError || request.result == UnityWebRequest.Result.ProtocolError)
Debug.Log(request.error);
else {
Texture2D tex = ((DownloadHandlerTexture)request.downloadHandler).texture;
spr.sprite = Sprite.Create(tex, new Rect(0.0f, 0.0f, tex.width, tex.height), new Vector2(0.5f, 0.5f), tex.width / 2f);
}
}
Is there a way to keep game running with user interactions(character movement or button actions) while downloading textures?
Unity co-routines (CR) are a entry-level and sometimes hazardous form of performing work over a series of frames on the Main Unity thread. They are useful for what is essentially a logical lerp across a series of frames such as fading, explode-my-spaceship-after-5-seconds, or deferring cockpit display updates.
CRs are sliced up and multiplexed over the Main Thread so the slowest part of your game is going to be the CR with the largest demand for time in a single frame. Performing anything lengthy during a single yield per frame is going to slow down your game, particularly I/O operations.
Alternative - Unity Jobs
Instead, consider using Unity 'IJob' which allows for operations to be executed on a worker thread thus freeing up the Unity thread to do what it does best and not lengthy I/O operations.
Unity:
Use IJob to schedule a single job that runs in parallel to other jobs and the main thread. When a job is scheduled, the job's Execute method is invoked on a worker thread. More...
Now wasting a whole thread that may spend it's life waiting for I/O to complete is arguably a waste of a perfectly good thread but it is certainly better than blocking the Main Thread.
Coroutines...thar be dragons
So earlier I mentioned "hazardous" with regards to CRs and the reason is three-fold:
When firing off a CR in a method that is called many times per second such as Update() make sure you adequately guard the StartCoroutine() else you could end up with zillions of them running only to run out of memory. This one is quite common on Stack Overflow
Don't make the mistake of thinking they are somehow a worker thread or other piece of magic that won't slow down the Main Thread
In many ways CRs are like DoEvents, Application.DoEvents in Visual Basic and .NET respectively and both leads to a nasty case of re-entrancy (where state is clobbered and unpredictable just like in a multi-threaded app but without the threads)
There's also the whole is Unity promoting a strange use of IEnumerator and yield? debate.
My other tip is, if you need something lerped or delayed and isn't doing I/O, consider just making a class and use Time.
e.g. in my flight sim, I only want to update cockpit displays every 100ms or so. For that I define a Delay class:
public class Delay
{
private float _lastInterval;
/// <summary>
/// The timeout in seconds
/// </summary>
/// <param name="timeout"></param>
private Delay(float timeout)
{
Timeout = timeout;
_lastInterval = Time.time;
}
public float Timeout { get; }
public bool IsTimedOut => Time.time> _lastInterval + Timeout;
public void Reset()
{
_lastInterval = Time.time;
}
public static Delay StartNew(float delayInSeconds)
{
return new Delay(delayInSeconds);
}
}
.
.
.
private void Update()
{
if (!_delay.IsTimedOut)
{
return;
}
// Do something time consuming
_delay.Reset();
}

Unity3D: Custom UnityEvent AddListener not firing

I have my own custom UnityEvent and am trying to add a listener.
I have used AddListener on numerous other UI objects, such as buttons, dropdowns, toggles, etc. so I understand the process. However, when I Invoke my UnityEvent, it simply doesn't fire.
I'm receiving no error messages, and after doing reading and research, everything looks correct. So, not sure what to do further.
This is an object that emits when it's rotated.
This is the basics of my code:
using UnityEngine.Events;
public class Rotator: MonoBehaviour
{
public UnityEvent OnRotate;
int angle = 0;
int newAngle = 0;
void Start()
{
OnRotate = new UnityEvent();
}
void Update()
{
newAngle = (int)transform.rotation.eulersAngles.z;
if (newAngle != angle)
{
print ("Actual Instance ID: " + GetInstanceID());
print ("Invoking!");
OnRotate.Invoke();
angle = newAngle;
}
}
}
and
public class Owner: MonoBehaviour
{
public Rotator rotator;
void Start()
{
print ("Rotator Instance ID: " + rotator.GetInstanceID());
rotator.OnRotate.AddListener(
() => UpdateRotation()
);
}
void UpdateRotation()
{
print ("Invoked!");
}
}
When the Rotator has it's angle changed, I get this in the console:
Actual Instance ID: 11234
Rotator Instance ID: 11234
Invoking!
The instance ID is to make sure I'm working with the same objects and not going in circles for nothing. They match, so I'm listening to the object that's firing.
However, the listener isn't firing. I've tried different combinations with delegates, etc. but it's all the same. No errors. It just doesn't invoke.
Obviously, I'm doing something wrong, but what is it?
Thanks for any help.
Somehow your answered your new edited version of the question with exactly the code you previously provided in the First Version of your Question!
As I tried to tell you ... if you anywhere in your code do OnRotate = new UnityEvent() of course you thereby erase any persistent callbacks and any runtime callbacks added before that moment!
In short
Simply leave it as
public UnityEvent OnRotate;
and you don't even have to think about it anymore.
For understanding why it also works if you put it in Awake please simply have a look at the Order of Execution for Event Functions
&rightarrow; First Awake and OnEnabled is called for every GameObject/Component. Then all Start methods are called as soon as the GameObject/Component is active.
Within each of these blocks (Awake + OnEnable) and (Start) the order of execution between different component types is not guaranteed unless you explicitly configure it via the Script Execution Order Settings where you could define that Owner is simply run before Rotator .. then having both in Start would also work again.
Why does it also work if you do it on the public field?
&rightarrow; Because this field is serialized. That means it is initialized automatically in the Inspector and then stored together with the Scene or prefab asset including any persistent callbacks.
And then Later Unity re-uses the serialized Version of the field so actually you can completely remove the new UnityEvent(); since it doesn't have any effect on a serialized field! It will always be initialized automatically anyway!
Ok, I found out what the issue was.
My question now is "why?".
I changed my code from:
public UnityEvent OnRotate;
void Start() {
OnRotate = new UnityEvent();
}
to
public UnityEvent OnRotate = new UnityEvent();
void Start() {
}
And now it works.
Although, now that I think about it, Awake() is the method where they all fire before initialization, whereas Start() is when the object is created. So the Start() of the Rotator is probably getting called after the Owner is adding a listener.

How to debug why Unity freezes?

I am doing a collaboration with my school mates for a school project, but recently when I run Unity it freezes and I can't get it to unfreeze. I have uninstalled and reimported all the assets and my group mates also said that their Unity does not have this problem. I can't find a solution.
The project is being done in 2d. I was testing some logic while this happened to me. I was making a health bar test where it automatically goes down and there's a text box while follows it containing the actual number. Then suddenly the next day Unity decided to quit on me and not run at all.
Update
Code:
public static float fillamount = 1f;
public float period = 1f;
public Image image;
private float time;
public float DoesItDecreaseOverTime = -0.1f;
private float tempnum;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
time += Time.deltaTime;
while (fillamount <100 || fillamount > 0)
{
if (time >= period)
{
fillamount += DoesItDecreaseOverTime;
time = 0;
}
}
tempnum = (fillamount * 100);
image.fillAmount = fillamount;
}
}
After long days of frustration myself, I have found how to debug while unity freezes. I was doing stuff with trees (not the vegetation, the data structure) and for me it was very hard to determine where and what causes stack overflow. The stack overflow freezes unity so I can't see any debug that would have helped me to solve the problem. But now, I found the solution.
SOLUTION
So, I tried using System.Diagnostics.Debug.WriteLine which you should see in the output window of Visual Studio. But that doesn't seem to work with a unity project. Anyways, I started to think a little bit and came up with this class:
using System;
using System.IO;
public class Logger
{
public Logger(string logFilePath)
{
if(!logFilePath.EndsWith(".log"))
logFilePath += ".log";
LogFilePath = logFilePath;
if(!File.Exists(LogFilePath))
File.Create(LogFilePath).Close();
WriteLine("New Session Started");
}
public string LogFilePath { get; private set; }
public void WriteLine(object message)
{
using(StreamWriter writer = new StreamWriter(LogFilePath, true))
writer.WriteLine(DateTime.Now.ToString() + ": " + message.ToString());
}
}
Usage is like this:
Logger logger = new Logger(#"C:\Users\Soogbad\Desktop\MyLog.log");
logger.WriteLine("Hello World");
This creates a log file in which you can see all the logs you made. It works perfect! Even while unity is freezing.
Try to avoid Loops in the update function (unless you fully control the loop), this function is called everyframe so if you perform long tasks in here you might experience framedrop and freezes.
In your case replace the while by a If and revert the conditions.
If you have unity pro you have the Profiler that will tell you what script/function is taking the most cpu time.

Plugin Development: IResourceChangeListener called more than once on just one change

I am using IResourceChangeListener to listen to changes in resource.
my code is:
public void createPartControl(Composite parent) {
// Graph will hold all other objects
graph = new Graph(parent, SWT.NONE);
// create the graph with the right nodes and connections.
init();
//listen to changes in the resource.
workspace = ResourcesPlugin.getWorkspace();
resourceChangeListener = new IResourceChangeListener() {
public void resourceChanged(IResourceChangeEvent event) {
//IResourceDelta delta = event.getDelta();
Display.getDefault().asyncExec(new Runnable() {
public void run() {
init();
System.out.println("Something changed!");
}
});
}
};
workspace.addResourceChangeListener(resourceChangeListener);
}
public void dispose() {
workspace.removeResourceChangeListener(resourceChangeListener);
}
But, when I am doing just one change in the resource, and save it, the listener called more than once! (usually twice, but sometimes even 6 times!!!)
I tried to use delta to see if it's called in the project,folder,file..
and I didn't saw differences between the calls (maybe I didn't used it properly).
I found this link, but I didn't found there solution to solve my problem
IResourceChangeListener being called twice with one change
How can I fix my code?
Thanks.
There is nothing you can do in the listener code to change how many resource change events you get. It depends entirely on how the code making the changes is doing the change.
You may find that IResourceDelta.getKind and IResourceDelta.getFlags return different values in the events. Also something getMovedFromPath and getMovedToPath for rename or move operations.
If it is your code doing the change you can use a WorkspaceJob or WorkspaceModifyOperation to do atomic changes to the workspace which will also reduce the number of resource change events.
You might also want to check that your old listeners are being removed correctly.

Best practices Implementing ProgressMonitorDialog in Eclipse RCP

I wish to Show progress of a long running operation(process) in UI, so that the user can understand the status of the background job. This is the way I have implemented and I feel like the code is absurd. Below is my code
dialog.run(true,false, new IRunnableWithProgress() {
#Override
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
monitor.beginTask("Main process", 10);
for (int i = 0; i < 10; i++) {
if (monitor.isCanceled()) return;
monitor.subTask("Status message");
sleep(1000);
// worked increases the monitor, the values are added to the existing ones
monitor.worked(1);
if(i == 3) {
sleep(3000);
callMe();//calling a long running function
}
if(i == 9) {
monitor.subTask("finishing setup..... please wait ");
sleep(2000);
}
}
monitor.done();
}
});
Note: There is a sleep method somewhere in the code
here at i == 3 an operation/function is called that takes a minimum of 5 minutes, post execution of the function the progress continues.
I don't want the progress to be stopped while executing the function(long running operation) rather progress must be shown even while executing it.
can someone show the correct programming practices in showing progress
The reason your code feels absurd is that wrapping the long-running method in a IRunnableWithProgress.run() really does not add much in itself, there is no magic.
To make the ProgressMonitorDialog (or e.g. the related Job API) work for you, you need to change "callMe()" so it takes "IProgressMonitor monitor" as a parameter, and use that to listen for cancel-requests, and use it also for reporting progress.
To say the same again, using different wording: You need to recursively add "IProgressMonitor monitor" as a parameter to all long-running method calls. All long-running operations must be build with this (IProgressMonitor) in mind if any progress is to be reported and/or you want it to be cancelable.