o A Descriptive Title (i.e. “Simulator Freezes Launching App in Eclipse”)
Constants and static enum variables appear to be instance variables
o The Environment:
Windows 10
Eclipse Neon.1 (4.6.1)
o A detailed description of the issue
Constants and static enum variables appear as instance variables, unexpectedly bloating object sizes.
o Steps to reproduce the issue
Compile and run the test case below. The output will indicate the increase in size shown for various declarations.
o Any applicable additional information
The compiler should be able to create shared instances of variables declared as `const' and `static enum', much like is done for `static const' and `static var' declarations. This should make it more difficult to waste memory accidentally. There could be some unintended side-effects with this change, so this might require a bit of testing to verify.
If this suggestion is rejected, the Programmer's Guide should add documentation for the `static', `const', `enum', and `var' keywords. It should mention that `static' is the only way to get a value that is shared between instances, and that using `static' and `const' together is a good idea to avoid incurring per-instance overhead.
o A code sample that can reproduce the issue (in email only if preferred)using Toybox.Application;
using Toybox.Lang;
using Toybox.System;
using Toybox.WatchUi;
class ClassWithNothing
{
}
class ClassWithConstValues
{
const VALUE_0 = 0;
}
class ClassWithStaticConstValues
{
static const VALUE_0 = 0;
}
class ClassWithStaticValues
{
static var VALUE_0 = 0;
}
class ClassWithValues
{
var VALUE_0 = 0;
}
class ClassWithEnumValues
{
enum {
VALUE_0
}
}
class ClassWithStaticEnumValues
{
static enum {
VALUE_0
}
}
var minimum_size;
function get_used_memory() {
var systemStats = System.getSystemStats();
return systemStats.usedMemory;
}
function do_test_type(logger, count, type, tname) {
var array = new [count];
var prev_used = get_used_memory();
for (var i = 0; i < count; ++i) {
array[i] = new type();
}
var next_used = get_used_memory();
var current_size = (next_used - prev_used) / count;
if (minimum_size == null) {
minimum_size = current_size;
}
logger.debug(Lang.format("$1$: $2$", [ tname, current_size - minimum_size ]));
}
(:test) function test(logger)
{
do_test_type(logger, 100, ClassWithNothing , " empty class");
do_test_type(logger, 100, ClassWithStaticConstValues, "static const");
do_test_type(logger, 100, ClassWithStaticEnumValues , " static enum");
do_test_type(logger, 100, ClassWithStaticValues , " static var");
do_test_type(logger, 100, ClassWithConstValues , " const");
do_test_type(logger, 100, ClassWithEnumValues , " enum");
do_test_type(logger, 100, ClassWithValues , " var");
return true;
}
class XApp extends Application.AppBase
{
function initialize() {
AppBase.initialize();
}
function getInitialView() {
return [ new WatchUi.View() ];
}
}
Related: https://forums.garmin.com/showthread.php?367568