2 Replies Latest reply: Jun 26, 2010 6:48 AM by 843829 RSS

    Java vs .NET: memory management battle

    800776
      Hello! I know that Java and .NET are similar in many aspects (cross-platform, garbage collection and so on...). I performed a little test, and I cannot explain it's results. Help me please with this.

      First I created simple java application (generated by NetBeans) with one empty JFrame:
      public class NewJFrame extends javax.swing.JFrame {
      
          public NewJFrame() {
              initComponents();
          }
      
          @SuppressWarnings("unchecked")
          private void initComponents() {
              setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
              javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
              getContentPane().setLayout(layout);
              layout.setHorizontalGroup(
                  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                  .addGap(0, 400, Short.MAX_VALUE)
              );
              layout.setVerticalGroup(
                  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                  .addGap(0, 300, Short.MAX_VALUE)
              );
              pack();
          }
          public static void main(String args[]) {
              java.awt.EventQueue.invokeLater(new Runnable() {
                  public void run() {
                      new NewJFrame().setVisible(true);
                  }
              });
          }
      }
      Then I created similar .NET application (generated by Ms Visual Studio 2005), here is C# code of the first source file:
      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Drawing;
      using System.Text;
      using System.Windows.Forms;
      
      namespace WindowsApplication1
      {
          public partial class Form1 : Form
          {
              public Form1()
              {
                  InitializeComponent();
              }
          }
      }
      The second source file:
      using System;
      using System.Collections.Generic;
      using System.Windows.Forms;
      
      namespace WindowsApplication1
      {
          static class Program
          {
              [STAThread]
              static void Main()
              {
                  Application.EnableVisualStyles();
                  Application.SetCompatibleTextRenderingDefault(false);
                  Application.Run(new Form1());
              }
          }
      }
      I cannot understand why there is such a difference in memory, consumed by these two applications:
      javaw.exe - 16804Kb
      WindowsApplication1.exe - 7608Kb
      Look here is my screenshot .

      PS: At my work, there is a guy who likes .NET (and Microsoft, I suppose). In our conversation I said that Java is better, because it's really multiplatform. He answered that Java is bad because it consumes a lot of memory and .NET does not. At this moment I do not what to answer... I'm very upset, because I like Java very much :(

      Sorry, I guess this subject is discussed a million times, but I still couldn't find satisfactory answer on this question.