Calculate available memory in Windows (python)

Someone asked something about determining free memory in python code. I was bored. Click through for more...
import os
import win32pdh
 
class PDHCounter:
    def __init__(self, query=None):
        self.query = query or win32pdh.OpenQuery(None, 0)
        self.counterPath = win32pdh.MakeCounterPath(
            (self.machineName,
            self.objectName,
            self.instanceName,
            self.parentInstance,
            self.instanceIndex,
            self.counterName), 0) #the 0 is for unused user data
            self.counter = win32pdh.AddCounter(
            self.query,
            self.counterPath,
            0) #0 again for unused user data
 
    def __str__(self):
        win32pdh.CollectQueryData(self.query)
        return self.format()
 
class AvailableMemoryCounter(PDHCounter):
    machineName = None
    objectName = 'Memory'
    instanceName = None
    parentInstance = None
    instanceIndex = -1
    counterName = 'Available Bytes'
 
    def format(self):
        #print win32pdh.BrowseCounters(None,0,browseCallback, win32pdh.PERF_DETAIL_WIZARD,'')
        data = win32pdh.GetFormattedCounterValue(self.counter, win32pdh.PDH_FMT_LONG)
        return '%dMB' % (data[1]/1024/1024)
 
class PythonProcessPID(PDHCounter):
    machineName = None
    objectName = 'Process Address Space' #this makes this an expensive (time) class
    instanceName = 'python'
    parentInstance = None
    instanceIndex = -1
    counterName = 'ID Process'
 
    def format(self):
        data = win32pdh.GetFormattedCounterValue(self.counter, win32pdh.PDH_FMT_LONG)
        return '%d' % (data[1])
 
class PythonProcessSpaceFree(PDHCounter):
    machineName = None
    objectName = 'Process Address Space' #this makes this an expensive (time) class
    instanceName = 'python'
    parentInstance = None
    instanceIndex = -1
    counterName = 'Bytes Free'
 
    def format(self):
        data = win32pdh.GetFormattedCounterValue(self.counter, win32pdh.PDH_FMT_LONG)
        return '%dMB' % (data[1]/1024/1024)
 
if __name__ == '__main__':
    print AvailableMemoryCounter()
    pidCounter = PythonProcessPID()
    spaceFreeCounter = PythonProcessSpaceFree()
 
    while str(pidCounter) != str(os.getpid()): #make sure we've got the right python.exe
        pidCounter.instanceIndex += 1
        spaceFreeCounter.instanceIndex += 1
    print '%s free address space for this process' % str(spaceFreeCounter)
 
    print 'allocating a large chunk...'
    x = ['abiglist' for y in range(1000000)]
    print 'now %s free address space for this process' % str(spaceFreeCounter)
    print 'freeing the chunk...'
    del x
    print 'now %s free address space for this process' % str(spaceFreeCounter)

Comments

cialis

viagra

Hello!
viagra , cialis , viagra , cialis , cialis ,

cialis

Hello!
cialis , cialis , viagra , cialis , viagra ,

Paxil

Hello!
Paxil ,

There is Always Help

viagra

Hello!
viagra , cialis , viagra , cialis , cialis ,

viagra

generic viagra

cialis

laptop batteries laptop

laptop batteries laptop batteries,
battery grip battery grip,
laptop batteries laptop batteries,
power tool battery power tool battery,
VGP-BPS8A VGP-BPS8A,
VGP-BPS8 VGP-BPS8,
VGP-BPS9/B VGP-BPS9/B,
VGP-BPS21A VGP-BPS21A,
Dell Latitude E4300 Battery Dell Latitude E4300 Battery,
SONY VGP-BPS21A SONY VGP-BPS21A,
Dell Precision M6400 Battery Dell Precision M6400 Battery,
Dell Precision M2400 Battery Dell Precision M2400 Battery,
Dell Precision M4400 Battery Dell Precision M4400 Battery,
Dell Latitude E6500 Battery Dell Latitude E6500 Battery,
Dell Latitude E6400 Battery Dell Latitude E6400 Battery,
Dell Inspiron 1750 Battery Dell Inspiron 1750 Battery,
Dell Inspiron 1440 Battery Dell Inspiron 1440 Battery,
LENOVO ThinkPad T410s battery LENOVO ThinkPad T410s battery,
ThinkPad T400s Battery ThinkPad T400s Battery,
HSTNN-IB99 HSTNN-IB99,
HSTNN-XB99 HSTNN-XB99,
Fujitsu FPCBP202 Battery Fujitsu FPCBP202 Battery,
Fujitsu FPCBP201 Battery Fujitsu FPCBP201 Battery,
Fujitsu LifeBook U2010 Battery Fujitsu LifeBook U2010 Battery,
LifeBook U2020 Battery LifeBook U2020 Battery,
LifeBook U820 Battery LifeBook U820 Battery,
Tecra A10 battery Tecra A10 battery,
ACER Ferrari 1100 Battery ACER Ferrari 1100 Battery,
AS09A61 AS09A61,
Aspire 1300 battery Aspire 1300 battery,
Dell XPS M2010 Battery Dell XPS M2010 Battery,
Dell XPS M1730 Battery Dell XPS M1730 Battery,
Dell Vostro 1710 battery Dell Vostro 1710 battery,
Dell Vostro 1720 battery Dell Vostro 1720 battery,

LaRLtPiyuEAFh

cialis

Hello!
cialis , cialis , viagra , cialis , ,

cialis

Hello!
cialis , cialis , viagra , cialis , ,

cialis

Hello!
cialis , cialis , viagra , cialis , ,

Women in summer would like to

Women in summer would like to become beautiful. Everything can grab other's eyes is their best friends.Products make them beauty and confident is their favourite. Look in the street,you can see many different types of make up to show women's personality.

Welcome to the shop, the following is our products, free shipping.

Soccer ShoesCheap Soccer ShoesNike Soccer ShoesAdidas Soccer ShoesNike Soccer Shoes saleAdidas Soccer Shoes saleUGGUGGsUGG BootUGG BootsUGG Boots SaleCheap UGG BootsUGG Boots CheapWomen UGG bootsugg boots cardyugg cardy bootsNike AirNike Air MaxNike Air Max ShoesNike SBNike DunkNike Dunk SBNike Dunk SB ShoesNike Shox Nike Shox ShoesTimberlandTimberland saleTimberland bootsTimberland boots onlineTimberland on saleNew timberland bootsWomen BagsWomen Bags SaleWomen HandbagsWomen Handbags SaleWomen New BagsCheap BagsCheap Bags On SaleUGGUGG bootsUGG boots saleUGG boots shortShort uggShort ugg bootsUgg boots tallNew women bagsNew women bags saleNew women bags sale onlineLouis Vuitton HandbagsGucci bagsNikeNike ShoesNike Shoes SaleNike runningNike running shoesNike trainersNike trainers shoesTimberlandTimberland bootsTimberland boots saleTimberland bootTimberland boot saleTimberland boots cheapMen timberlandsMBTMBT ShoesMBT Chapa GTXMBT Men ShoesMBT Women ShoesDiscount MBT ShoesLV HandbagsGucci HandbagsChanel HandbagsChloe HandbagsD&G HandbagsDior HandbagsFendi HandbagsHermes HandbagsJimmy Choo BagsMarc Jacobs BagsMiu Miu HandbagsMulberry BagsPrada HandbagsVersace HandbagsYves Saint LaurentBalenciaga BagsBurberry HandbagsLV HandbagsGucci HandbagsChanel HandbagsChloe HandbagsD&G HandbagsDior HandbagsFendi HandbagsHermes HandbagsJimmy Choo BagsMarc Jacobs BagsMiu Miu HandbagsMulberry BagsPrada HandbagsVersace HandbagsYves Saint LaurentBalenciaga BagsBurberry Handbags

Those who want to become most beautiful in the world should try them. Just ones can make you different. Girls who want to grab your boyfriends's heart is necessary to use them.

cialis

Hello!
cialis , cialis , viagra , cialis , ,

cialis

Hello!
cialis , cialis , viagra , cialis , ,

cialis

Hello!
cialis , cialis , viagra , cialis , ,

cialis

Hello!
cialis , cialis , viagra , cialis , ,

cialis

Hello!
cialis , cialis , viagra , cialis , ,

cialis

Hello!
cialis , cialis , viagra , cialis , ,

cialis

Hello!
cialis , cialis , viagra , cialis , ,

cialis

Hello!
cialis , cialis , viagra , cialis , ,

cialis

Hello!
cialis , cialis , viagra , cialis , ,

cialis

Hello!
cialis , cialis , viagra , cialis , ,

cialis

Hello!
cialis , cialis , viagra , cialis , ,

viagra

Hello!
viagra ,

cialis

Hello!
cialis ,

replica handbags

Intime:2010-2-25the replica handbags pleasure at leastWhen you carry a LV Nomade replica handbags, you?ˉll instantly get noticed, by men and women alike. Your sleek accessory is sure to serve as a conversation point, inspiring admiring compliments and covetous glances.In addition to reflecting your high sense of fashion, a LV old replica handbags also represents prestige, status, and confidence. For many who are on a budget, however, a genuine LV Seasonal Collection replica handbags can be a cost-prohibitive purchase. In today?ˉs struggling economy, not many women can afford to shell out six months?ˉ salary for the steep price tag of a real LV Suhali leather replica handbags. Of course, the premium materials and careful craftsmanship infuse the LV Taiga Leather replica handbags with more value, but it may not be prudent to drain your savings account to purchase one.Fortunately for those women who love the look of Marc jacob replica handbags but not the high prices, we?ˉve found a way to grant them their fashion wish without busting their budget.We offer a wide selection of meticulously replicated Miu Miu replica handbags at Ereplica-shop.com. In today?ˉs tight times, thousands of women are saving thousands of dollars by purchasing our carefully duplicated louisvuittonstuff.com Mulberry replica handbags.We?ˉre well aware of the ?°bait and switch?± marketing tactics of most Paul Smith replica handbags merchants. Unfortunately, they often use false advertising to portray an artificial quality. At Valentino replica handbags?ˉre always completely up-front with our customers about the product they?ˉll receive. On our website, we display up-close images and detailed descriptions of each Prada replica handbags and imitation wallet. Our Thomas Wylde replica handbags is to please each and every one of our customers to keep them coming back for more. We?ˉre not out to just make a quick sale?ainstead, we?ˉre interested in forging long-term relationships to provide you with a lifetime of high-quality accessories.

viagra

Hello!
viagra , viagra , cialis , cialis , ,

witRuMlfbxZWY

viagra

Hello!
viagra , viagra , cialis , cialis , ,

dGGaIauGAFFL

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <p> <div> <span>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
3 + 0 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.