Android Question Memory Management

AlexOfOz

Active Member
Licensed User
As always, this might be a naive question with a simple answer.

My program uses a lot of panes and 2 dimensional arrays to manage their content. But the memory on my phone keeps filling up and everything stops.

What commands are there for me to be able to clear the memory, or at least manage the memory?

Thanks.
 

Brian Dean

Well-Known Member
Licensed User
Longtime User
I don't have any special knowledge in this area, but I do not like to see questions go unanswered. If I say something wrong then maybe somebody more knowledgeable will correct me and we will both learn something.

First of all, I am sure that you cannot influence the Java garbage collector. It will just do its job when there is work to be done. I think that in some circumstances you can request a larger heap, but I feel that will not help here. I think that it is far more likely that you are not using the available memory efficiently. You don't mention images, so I guess that they are not part of this problem.

Secondly, what do you think that the operating system could do to help? You say that you are using "lots of panes" (like 500? or 5000? or 50000?). How many of these panes are visible at any time? If it is several hundred then maybe your app is a game and you should be using a game engine approach. If the panes are large enough to have content and you only show a few of them at a time then maybe you should be reusing panes or using some lazy-loading technique.

I think that you need to tell us more. What would you like to be able to do to "manage the memory"? Why do you need so many panes?
 
Upvote 0

AlexOfOz

Active Member
Licensed User
Brian, thankyou for your reply. Your questions have helped to guide me to the solution. The answer to my question is that I must exit the program cleanly and let the operating system look after itself.

My program is purely for personal fulfillment and use. It is a minesweeper game and uses a 20 x 20 matrix of panes for the game board. After your guiding questions, I have already rearranged how I manage the information for each cell on the game board, thereby reducing the impact on memory. Now I will ensure that I exit cleanly to (hopefully) solve the rest of the problem.

Always learning is the best thing I gain from this.

Alex

PS - I've just made the change to let the operating system look after it and it now works fine. Thankyou for your guiding questions.
 
Last edited:
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
That all sounds good - you should not expect to be running into problems with 400 panes.
Always learning is the best thing I gain from this.
Something that you might like to consider if you are interested in looking at different ways to do things : Some years ago I wrote a Suduko game (as, I suspect, many people do). I used a 9x9 array of buttons for the cells as that seemed to be the obvious way to start. But I never really succeeded in getting the text properly centred, and grouping the cells into blocks of nine never looked quite right, and results on some devices were really poor. Several years later, when I had learned more, I used a canvas, overlaid with a single transparent panel to receive the touch actions. Drawing the cells on a canvas produced a very much better visual appearance, and processing the touch events also became easier. In fact many aspects of the game became easier to code. I mention this only because a minesweeper game has obvious similarities, and maybe the obvious solution (building the board out of 400 panels) is not the best one.
 
Upvote 0

AlexOfOz

Active Member
Licensed User
Brian, it's funny how we seem to eventually find that hidden solution for any given problem.

I have already learned to use the transparent overlay panel to catch the touch actions. Oh, and I have a successful sudoku, just for my own use. The interesting part of creating that was to ensure that it was random, combined with the need to ensure that all nine sections had all the numbers, as did all of the lines etc. I created a way of ensuring that which uses a bunch of randomisers and lots of round and round we go. The end result is a working sudoku, which I'm quite proud of.
 
Upvote 0

AlexOfOz

Active Member
Licensed User
Thanks Erel. It wasn't actually an error as the program didn't "fail". It was just when I tried to start the app on my phone again, it sat there with a blank screen and refused to do anything. The only way past that point was to restart the phone.

But from the advice above, I have now moved past that and have a functioning program. I had to introduce a stop button, telling the program that I was finished playing, and it removes a bunch of stuff before then doing an Activity.Finish. I believe it is that command that handles the memory problem and allows me to start the program again without fuss.

This forum is excellent in that simple assistance such as that above goes a long way to help amateurs such as myself gain knowledge and confidence.
 
Upvote 0
Top