Skip to content

Instantly share code, notes, and snippets.

@ajaynitt
Created October 25, 2018 04:49
Show Gist options
  • Save ajaynitt/c0e1c26237e2f805f2d6db085f3a07e1 to your computer and use it in GitHub Desktop.
Save ajaynitt/c0e1c26237e2f805f2d6db085f3a07e1 to your computer and use it in GitHub Desktop.
object creation - memory leavel info in java
public class MyProgram {
public static void main(String[] args) {
// Create an 'MyObject' for the first time the application started
MyObject obj = new MyObject();
}
}
/*
When an object from the MyObject class is created for the first time, the JVM searches the file system for the definition of the class, that is the Java byte code. The file has the extension of *.class. The CLASSPATH environment variable contains locations where Java classes are stored. The JVM is looking for the MyObject.class file. Depending on which package the class belongs to, the package name will be translated to a directory path.
When the MyObject.class file is found, the JVM's class loader loads the class in memory, and creates a java.lang.Class object. The JVM stores the code in memory, allocates memory for the static variables, and executes any static initialize block. Memory is not allocated for the object member variables at this point, memory will be allocated for them when an instance of the class, an object, is created.
There is no limit on how many objects from the same class can be created. Code and static variables are stored only once, no matter how many objects are created. Memory is allocated for the object member variables when the object is created. Thus, the size of an object is determined not by its code's size but by the memory it needs for its member variables to be stored.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment