2

In this code:

Properties prop = new Properties();
prop.load(new FileInputStream("config.properties"));

Some properties are loaded, but does the fileinputstream need to be closed or does it somehow take care of that itself?

Do I need to create a variable, new file inputstream, then close the variable?

I was also wondering, if I create a variable, say, String a = null and int b; Do they consume memory when they hold nothing?

and if I have that inside a method or a loop, does it still consume memory when out of scope?

I think someone once said it's loaded into memory but not 'active'?

RanRag
  • 48,359
  • 38
  • 114
  • 167
user1021085
  • 729
  • 3
  • 10
  • 28
  • 3
    Please split these into multiple questions :). Also you may search and find they are answered elsewhere. – Garrett Hall Jan 26 '12 at 20:31
  • The stream question was answered, could someone answer the memory one? It might be online but it's often written in some stupid and complicated way. I'd prefer someone exlplaining – user1021085 Jan 26 '12 at 20:33

5 Answers5

3

The JVM will likely quickly garbage collect and close the FileInputStream. In a small script, this is fine to do. However, the canonical idiom goes:

FileInputStream fis;
try {
    fis = new FileInputStream("config.properties");
    prop.load(fis);
} finally {
    fis.close();
}

As for variables, each variable is a reference and will take up 8 bytes of memory even if it stores nothing (ints will generally only take up 4 bytes, and booleans are even more optimized, but these are special cases). Once the declaration leaves scope, these local references will take up no memory (since the stack space for them is gone) but any objects you create will exist until they are garbage collected.

stefan
  • 1,511
  • 9
  • 13
  • this wont complie. `fis=...` has to be outside try, yeah – bestsss Jan 26 '12 at 20:37
  • Not sure what compiler you're using? This is definitely proper syntax and compiles in both OpenJDK and SunJDK. Some versions might complain if you don't initialize with `FileInputStream fis = null;` – stefan Jan 26 '12 at 20:43
  • Closing the stream also flushes its contents, so even small scripts should close the stream to ensure that everything gets written to disk (calling flush() directly also works). – josefx Jan 26 '12 at 21:09
  • I agree with using this idiom even in small scripts, but the JVM calls close on the stream when it is finalized, which also flushes the stream, so this isn't a problem (doubly not a problem here since it's an input stream ;) ) – stefan Jan 26 '12 at 21:18
  • @stefan the jvm wont run finalizers before exiting so your streams wont be flushed when used in a short script. The system class had the method runFinalizersOnExit to work around this behavior but it has been deprecated (threading issues). As you point out this is not a problem for InputStreams – josefx Jan 27 '12 at 19:43
1

Yes, you definitely need to close the stream yourself. Im referring here to the java documentation: click! which says

The specified stream remains open after this method returns.

poitroae
  • 21,129
  • 10
  • 63
  • 81
1

It is always better to close InputStreams (if not some point of time those will be GCed, but we don't know when it will be, which may cause whole lot of issues).

String a = null and int b

When you define like above memory is not allocated in String case.

kosa
  • 65,990
  • 13
  • 130
  • 167
1

Streams: Yes. Java doesn't have destructors, so objects can't take care of their own cleanup. Some amount of cleanup is done at garbage collection time (finalizers), but it's not good programming practice to rely on that.

One of the reason "finally" blocks exist in Java is to take care of resource deallocation.

Memory allocation: looks like it does't. I created the following program:

public class deleteme
{
    public static void main( String[] args )
    {
        int a;
        String s;
    }

}

Compiled it, then decompiled using javap -c, and got:

public class deleteme {
  public deleteme();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: return
}

Looks like nothing really happens, except for initializing my main class.

Then i changed the code to say:

int a = 1;
String s = "";

compiled, decompiled and got:

public class deleteme {
  public deleteme();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: iconst_1
       1: istore_1
       2: ldc           #2                  // String
       4: astore_2
       5: return
}

You can clearly see the additional instructions in the "main" method, where the memory is allocated.

I have a feeling that different versions the Java compiler might handle this differently.

theglauber
  • 28,367
  • 7
  • 29
  • 47
0

That was a lot of questions in one question :)

Streams should always be closed, a lot of answers for this already for example

Community
  • 1
  • 1
Peter Svensson
  • 6,105
  • 1
  • 31
  • 31