0

is there a way to tell the compiler in Java or Android to not remove some statements in code -which are intended to clean up variables after use to prevent any data remnant in ram-?? would creating a dummy method solve this issue??

these statements basically set the variables to their type-based initial values..

Thanks in advance!

Jana
  • 63
  • 8
  • 4
    Can you show us what these statements look like? – Keppil Feb 12 '14 at 16:03
  • You do not need to clean up variables. As soon as they are out of scope, they will be GCed. – Simon Feb 12 '14 at 16:03
  • 2
    My hunch is that Jana is after security not after memory. – Harald Feb 12 '14 at 16:05
  • 1
    In that case, it might be good to note that setting variable to null doesn't necessarily mean that it's no longer in ram. There might be a delay before they are GC. – Kuba Spatny Feb 12 '14 at 16:09
  • a/ you do not need to clean variables your self, unless your scopes are messed up. b/ dead code is dead, it is never executed, that's why it is dead. – njzk2 Feb 12 '14 at 16:10
  • @Harald Good point. But you need to do a lot more than null out vars if you care. Nulling out will do nothing to remove the value from memory. It will simply make it available for use again. There is no guarantee, nor way to know, that the memory has been reused - and even it is, has been overwritten. There are specific algorithms to clear the heap, it is more complex again if we are talking stack. – Simon Feb 12 '14 at 16:21
  • @Simon I absolutely agree. I was just waiting to see some code to be sure where this was heading. If it is basic types that need to go, there is a chance you can keep it under control, but setting pointers to null does of course not help. – Harald Feb 12 '14 at 16:23
  • Thank you all for responding! as Harald said I am after security, so for example if we got: String password, we should reset it to password = ""; right after its use.. – Jana Feb 17 '14 at 17:32
  • Also the resetting process is limited to variables of basic datatypes not references.. – Jana Feb 17 '14 at 17:48

2 Answers2

0

I answer under the odd assumption that you have a good reason to believe that the code is still useful even though it is dead.

Store the value false in some obfuscated form that the compiler can't understand. Then, conditionally branch to that code using your obfuscated value. The compiler will not know it is dead, so it will not be removed.

I'll use a file for my example, but it is probably not the most efficient way. Say your code that the compiler thinks is dead code was in a function called myCode(). Assume that fin is reading from a file that only contains false followed by EOF

if(Boolean.parseBoolean(fin.next()))
    myCode();
Rainbolt
  • 3,542
  • 1
  • 20
  • 44
  • sorry for not being clear enough and misunderstanding the dead-code concept! thanx! – Jana Feb 17 '14 at 17:44
0

The code that you describe is not dead code. Dead code is code that will never execute. Here is an example:

private int secretSchmarr;

public boolean blammo()
{
    boolean returnValue;

    secretSchmarr = calculateSecretValue();

    returnValue = useSecretValue(secretSchmarr);

    secretSchmarr = 99; // this is not dead code.

    return returnValue;

    secretSchmarr = 98; // This is dead code because it can never execute.
}
DwB
  • 37,124
  • 11
  • 56
  • 82
  • thanx for explaining that! however, is there any guarantee that the compiler will execute "secretSchmarr = 99; // this is not dead code." even though there is no uses associated with this definition?? as I checked in the debugger the compiler executed the statement yet I am not sure whether this is always the case!! – Jana Feb 17 '14 at 17:40
  • Baring exceptions thrown from `calculateSecretValue` and `useSecretValue` the line `secretSchmarr = 99` is guaraneteed to execute before the `return returnValue` line. – DwB Feb 17 '14 at 17:55