2

I just made a program that takes user input in the form of a float and converts the number from MM to inches. I recently noticed that when I run the program the more the program loops (takes user input) The more memory the program starts to use up. How can I make it so that it doesn't use more memory every time it takes user input? I should add that I am very new to programming, python is the first language I started teaching myself, I'm not using books other than the Python documentation, and I am mostly just going through other peoples source code and running it/editing it to figure out whats doing what. This is the first program I wrote on my own. And I have only been teaching myself Python for a little under 3 weeks Here's the code

Inches = float(25.4)
Surface_Inches = "**********Surface in Inches**********"
Enter_Number = "**********Enter Number in MM**********"
Final_Number = "**********Number in Inches**********"
WTF = "Sorry I could not recognize that, please try again"
keep_running = True
help = """
        Commands:
        type (exit, quit, end, or kill) to exit the program
        type RA to enter Surface finish conversion
        If you are in Surface conversion mode
        you must enter Ra number in MM that
        needs converted to Inches.
      """

import time
import math
import os

print("Welcome to the converter")
time.sleep(.75)
print("I can convert units of measurement from MM to Inches")
time.sleep(.75)

while keep_running:
    print("Enter Number or type RA for Surface conversion")
    print(Enter_Number)
    number = (input())
    if number in('end', 'quit', 'exit', 'kill'):
        break
    elif number in('clear'):
        os.system('CLS')
    elif number in('help'):
        print(help)
    elif number in('Ra', 'RA', 'ra', 'rA', 'Surface', 'Finish'):
        print("Enter the Surface Finish in MM")
        Surface = (input())
        if Surface in('help'):
            print(" ")
            print(help)
            print(" ")
        else:
            try:
                Surface_Finish = float(Surface)
            except ValueError:
                print(WTF)
            else:
                Surface_IN_Finish = Surface_Finish/Inches
                Answer = (float(Surface_IN_Finish *1000))
                if Answer < 1 : #if the converted results is less than 1 the answers 1
                    Answer = 1
                print(Surface_Inches)
                print(" ")
                print('\t', "        %.0f" % Answer)
                print(" ")
                print(Surface_Inches)
else:
    try:
        MM = float(number)
    except ValueError: #checks to see if the user input is a number
        time.sleep(0.5)
        print(WTF)
    else:
        Results = float(MM \ Inches)
        Final_Form = ("%.3f%" % Results) #creates a float that goes to 3 decimal places
        print(Final_Number)
        print(" ")
        print('\t', "        ", Final_form)
        print(" ")
        print(Final_Number)
        from tkinter import Tk #Imports the Tk Module
        final_form2 = (str(final_form))
        r = Tk()
        r.withdraw() # I have no idea what this does
        r.clipboard_clear()
        r.clipboard_append(final_form2) #appends final_form2 to clipboard 
Inhale.Py
  • 260
  • 1
  • 5
  • 15
  • Python as a good gc http://stackoverflow.com/questions/4484167/details-how-python-garbage-collection-works, so I am not sure how you are measuring up the memory usage. This seems like a simple program to observe any noticeable memory degradation. Can you tell how you measured the memory usage? – Bhavish Agarwal May 01 '13 at 11:40
  • @ Bhavish Agarwal I go into the task manager and find my program under processes (I built it into a .exe using cx_Freeze) When I first open my program its memory is at 4,692K If I enter a number to be converted lets say 10 times its memory usage goes up to 21,528k. It just keeps rising the more I use the program. Yesterday I was running it all day and by the end of the day it was taking up almost 1gb of ram. Is there a way I can stop it from doing this – Inhale.Py May 01 '13 at 11:54
  • Try running the app using python interpreter. The memory leak might be intorduced by cx_Freeze. – J0HN May 01 '13 at 11:56
  • @J0HN I plan on distributing a copies of this to some fellow employees after I get it polished. We currently convert a lot of units of measurement from MM to Inches and then enter that data elsewhere. So this program will come in handy for us and I need it to be a .exe – Inhale.Py May 01 '13 at 12:01
  • Ok, anyway, run it under python interpreter just to get an idea where the memory leak is: in python code or in exe generated by cx_Freeze – J0HN May 01 '13 at 12:12
  • @J0hn 5 Just tried running it in python interpreter and I'm still getting the same results – Inhale.Py May 01 '13 at 12:18
  • Ok, what's the `final_form`? Looks like you are creating a Tkinter form each time you convert something. Does that form appear on the screen? If so, do you close it? – J0HN May 01 '13 at 12:21
  • the final_form is just a variable I use to get the number to the right number of decimal places. I am only using tkinter to copy a number to the windows clipboard so that It can just be pasted into lets say an excel sheet this way the user doesn't have to type out the converted number thus eliminating 50% of the human error (The user cant accidentally mistype the converted number all he/she has to do is hit ctrl V) – Inhale.Py May 01 '13 at 12:37

0 Answers0